Re: Add a line after each 1000 records (452 Views)
Reply
Frequent Advisor
Muktha
Posts: 47
Registered: ‎02-14-2008
Message 1 of 10 (452 Views)

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
Please use plain text.
Honored Contributor
Kenan Erdey
Posts: 559
Registered: ‎02-13-2006
Message 2 of 10 (452 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 10 (452 Views)

Re: Add a line after each 1000 records

Hi Muktha:

One way:

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

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 10 (452 Views)

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...
Please use plain text.
Super Advisor
AZayed
Posts: 391
Registered: ‎04-12-2008
Message 5 of 10 (452 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 10 (452 Views)

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...
Please use plain text.
Frequent Advisor
Muktha
Posts: 47
Registered: ‎02-14-2008
Message 7 of 10 (452 Views)

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

Please use plain text.
Frequent Advisor
Muktha
Posts: 47
Registered: ‎02-14-2008
Message 8 of 10 (452 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 25,007
Registered: ‎03-06-2006
Message 9 of 10 (452 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 10 (452 Views)

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...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation