Insert a line of text every nth line

Insert a line of text every nth line

Post by Tom Urbanowi » Wed, 14 Aug 2002 05:09:17



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.

 
 
 

Insert a line of text every nth line

Post by Andreas Schwa » Wed, 14 Aug 2002 05:49:38


|> 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?

$ awk '{ print }; NR % 1000 == 0 { print "COMMIT;" }' < in > out

Andreas.

--

SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nrnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

 
 
 

Insert a line of text every nth line

Post by William Par » Wed, 14 Aug 2002 06:28:58




> |> 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?

> $ awk '{ print }; NR % 1000 == 0 { print "COMMIT;" }' < in > out

Above answer may be the most logical solution.  For completeness, you can
do the same thing in shell,
    i=0
    while read a; do
        echo $a;
        if [ $((++i%1000)) -eq 0 ]; then
            echo COMMIT;
        fi
    done

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin

 
 
 

Insert a line of text every nth line

Post by John W. Krah » Wed, 14 Aug 2002 07:15:53



> 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);

perl -i~ -pe'$. % 1000 or print"COMMIT;\n"' yourfile

John
--
use Perl;
program
fulfillment

 
 
 

Insert a line of text every nth line

Post by Stephane Chazela » Wed, 14 Aug 2002 17:03:21




> 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);

Just for the fun of it:

sed -e 'x
        s/$/1/
        s/1111111111/2/
        s/2222222222/3/
        /3333333333/{
          s///
          a\
COMMIT
        }
        x'

Of course, awk is definitely the best tool do this.

--
Stphane

 
 
 

Insert a line of text every nth line

Post by John W. Krah » Wed, 14 Aug 2002 20:47:53



> Just for the fun of it:

> sed -e 'x
>         s/$/1/
>         s/1111111111/2/
>         s/2222222222/3/
>         /3333333333/{
>           s///
>           a\
> COMMIT
>         }
>         x'

> Of course, awk is definitely the best tool do this.

             ^^^
You spelled perl incorrectly.  :-)

John
--
use Perl;
program
fulfillment

 
 
 

Insert a line of text every nth line

Post by Stephane Chazela » Wed, 14 Aug 2002 21:30:47


[...]

Quote:>> Of course, awk is definitely the best tool do this.
>             ^^^
> You spelled perl incorrectly.  :-)

In comp.lang.perl, I would agree. But in comp.unix.shell, even if you
find shorter than "awk '1;!NR%1000{print"COMMIT"}'", I still find awk is
the best tool.

I appreciate to see what would be the perl equivalent for this or that
script, but keep in mind that generally, in comp.unix.shell, people are
not looking for perl solutions.

--
Stphane

 
 
 

1. sed query ... printing every nth line of text file

An ultra-naive question ... how can I use sed, or equivalent, to strip out
every 10th line, say, of an enormous text file, and dump it to another new
file?

Thanks in advance for any clues ...

Cheers,
  Brad

------------------------------------------------------------------------------

  Dept. of Astrophysics
  Keble Road                                    PHONE:    +44-(0)1865-273292
  University of Oxford                          FAX:      +44-(0)1865-273390
  Oxford, England                    
  OX1 3RH
                     URL: http://www-astro.physics.ox.ac.uk/~bkg/gibson.html
------------------------------------------------------------------------------

2. Plug & Pray ???

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

4. RedHat Up2Date problem

5. Delete a whole line with sed, then insert diff text on same line...

6. ftpd pid file error message

7. How do I insert line numbers before each line using ed?

8. CD-ROM and HDD Probs

9. how to insert line number in front of each line

10. Vi -- How to Insert a Blank Line to all existing lines

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

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

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