Inserting Spaces At Column (x) Of Every Line In A File

Inserting Spaces At Column (x) Of Every Line In A File

Post by bai.. » Sun, 12 May 1996 04:00:00



        I've always wondered if there is a slick way of entering spaces at a
certain column number of every line in a file.  For instance, I want
to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

-- Responses Appreciated --
Andy

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by Bill Marc » Sun, 12 May 1996 04:00:00



>    I've always wondered if there is a slick way of entering spaces at a
>certain column number of every line in a file.  For instance, I want
>to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

I'm not sure whether you want to insert spaces or change the existing
characters to spaces, but either way you could use awk:
awk '{print substr($0,1,9) "           " substr ($0,21)}' file > newfile
change the "21" to "10" if you want to add spaces without losing the
existing contents of columns 10 to 20.

--

On 22 July, 1996, at 6:00 pm GMT, everyone in the world    
just START HUMMING.  Those who don't know will freak.      

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by Louis L » Sun, 12 May 1996 04:00:00



>I've always wondered if there is a slick way of entering spaces at a
>certain column number of every line in a file.  For instance, I want to
>enter spaces from column 10 to 20 in foo.txt.  How do I do it?

Are you trying to insert 10 spaces starting at column 10, or replace
columns 10-20 with spaces?

Louie
--
  ...who sometimes writes as if he's just recording his stream of
  consciousness because he thinks it's a fascinating concept that
  so many seemingly disjoint thoughts & ideas can come rushing one
  after another without form or connection but must yet be somehow
  related in a not-so-random fashion in his subconscious.  How scary.

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by Randal L. Schwar » Tue, 14 May 1996 04:00:00


Invisible_Man>       I've always wondered if there is a slick way of entering spaces at a
Invisible_Man> certain column number of every line in a file.  For instance, I want
Invisible_Man> to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

        perl -lape 'substr($_,10,0) = " " x 10 if length > 10' <infile >outfile

Just another Perl hacker,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by Rod Cerkon » Wed, 15 May 1996 04:00:00


:       I've always wondered if there is a slick way of entering spaces at a
: certain column number of every line in a file.  For instance, I want
: to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

Try the command line version of printf:

        printf "%9s%11s%-10s\n" "foo" "" "bar"
        #
        # foo is left justified in spaces 1-9
        # null, "", produces spaces in 10-20
        # bar is right justified starting in position 21
--

-rc-, Walk on!

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by John Savag » Fri, 17 May 1996 04:00:00



>I've always wondered if there is a slick way of entering spaces at a
>certain column number of every line in a file.  For instance, I want
>to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

Instead of using slick how about sed?  :)

sed 's/........./&          /' foo.txt >file2
or,
sed 's/.\{9\}/&          /' foo.txt >file2

There might be some places where tabs in the input need to be watched.
--

 
 
 

Inserting Spaces At Column (x) Of Every Line In A File

Post by Dan Whi » Sat, 18 May 1996 04:00:00



>    I've always wondered if there is a slick way of entering spaces at a
>certain column number of every line in a file.  For instance, I want
>to enter spaces from column 10 to 20 in foo.txt.  How do I do it?

>-- Responses Appreciated --
>Andy

In vi you could do this:
:s/^........../&          /

and that would insert 10 spaces.  That can be mutated into a sed
or awk script or some such.

There would be a problem if there were tab characters in there.
One character spans multiple columns, so maybe you could "de-tab"
the file first, insert spaces, then "re-tab"

------------------------------------------------------------------------
                                                               Dan White
------------------------------------------------------------------------
"Sometimes  I  think  the  surest  sign  that  intelligent  life exists
 elsewhere  in the universe is that none of it has tried to contact us."
                                                  Calvin (Bill Waterson)

 
 
 

1. Insert a line of text every nth line

I am working with a SQL file consisting of 50,000+ INSERT statements.
I would like to insert a COMMIT; statement every 1,000 lines. How can
I accomplish this in ksh using a loop function? Can it be effectively
accomplished in awk?

CURRENTLY:
Line 999  Insert into test (cola, colb, colc) values (1,2,3);
Line 1000 Insert into test (cola, colb, colc) values (4,5,6);
Line 1001 Insert into test (cola, colb, colc) values (7,8,9);

SHOULD BE:
Line 999  Insert into test (cola, colb, colc) values (1,2,3);
Line 1000 Insert into test (cola, colb, colc) values (4,5,6);
Line 1001 COMMIT;
Line 1002 Insert into test (cola, colb, colc) values (7,8,9);

Thanks in advance.

2. pcmcia: add device_class pcmcia_socket, update devices & drivers

3. inserting a blank line after every line - using sed

4. libranet 2.0

5. how to insert a set of lines in a file after last address and quit inserting

6. KDE 2.2 and RedHat 6.2

7. replacing characters at column "x" every "n'th" line

8. Netscape

9. Inserting a comma "," in the end of every line ( no matter what ).

10. Insert a line at the first line without a # in a file

11. Vi command to insert at the begin of a line for lines in a file

12. Inserting newline after every 4000 bytes in a file

13. HELP: How do I read a file line-by-line without getting spaces stripped?