Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Add a line after each 1000 records

Muktha
Frequent Advisor

Add a line after each 1000 records

Hi all,

There are 10000 records in a file(10000 lines).I want to add a line after each 1000 records .Could you please help me to do?

Regards
Muktha
9 REPLIES
Highlighted
Kenan Erdey
Honored Contributor

Re: Add a line after each 1000 records

Hi,

awk '{print $0;if (NR%1000==0) print "1k line seperator";}' input_file > output_file

Kenan.
Computers have lots of memory but no imagination
James R. Ferguson
Acclaimed Contributor

Re: Add a line after each 1000 records

Hi Muktha:

One way:

# perl -ple 'print "INSERTED" if ($. > 1 && $. % 1000 == 1)' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Add a line after each 1000 records

Hi (again):

Now that I'v had coffee, this is much more consise:

# perl -ne 'print $_;print "INSERTED\n" if $. % 3 == 0' file

Should you desire to update in-place simply do:

# perl -ni.old -e 'print $_;print "INSERTED\n" if $. % 3 == 0' file

Your original 'file' will be preserved as 'file.old'.

Regards!

...JRF...
AZayed
Super Advisor

Re: Add a line after each 1000 records

Dear JRF,

Perl is not pre-installed in HP-UX by default, isn't it ?

Thanks
Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit.
James R. Ferguson
Acclaimed Contributor

Re: Add a line after each 1000 records

Hi (agan):

> Perl is not pre-installed in HP-UX by default, isn't it ?

There should be a viable copy of Perl on your server even if it is a bit old. Find it with:

# whereis perl

Regards!

...JRF...
Muktha
Frequent Advisor

Re: Add a line after each 1000 records

Hi,
Thanks to all.
Sorry to say that ,we should not use perl commands in our scripts.

Regards
Muktha

Muktha
Frequent Advisor

Re: Add a line after each 1000 records

Thank you very much Kanan...
awk command works fine.
And sorry to disturb you again. Could you please explain that?

Regards
Muktha
Dennis Handly
Acclaimed Contributor

Re: Add a line after each 1000 records

>awk '{print $0; if (NR % 1000 == 0) print "1k line separator"}' input_file
>Could you please explain that?

It prints the current record. If the current input record number (NR) divided by 1000 has a remainder 0, then print out a separator.
James R. Ferguson
Acclaimed Contributor

Re: Add a line after each 1000 records

Hi (again) Muktha:

> Sorry to say that ,we should not use perl commands in our scripts.

And just why would that be? The reason *can't* be portability since Perl has been ported to more platforms than most of us can even name:

http://www.cpan.org/ports/index.html

To deny yourself the use of an extremely powerful tool is crippling at best. Assembly language, anyone?

Regards!

...JRF...