Inserting header after every nth line

Hi Experts,

I have a file which contain hundreds of records/lines. I want to insert the below header in the file after every 60 lines.

#Header

FirstName              LastName                Address          
---------              ----------              ---------

Let say I saved the above strings into a file called "header.txt".

Is there a way to just cat/echo/paste it into the main file after every 60 lines?

Thanks in advance!

try..

 
awk '{if(!NR%60){print "#Header">"filename"}{print $0>"filename"}}' originalfile

Try something this..

var="FirstName              LastName                Address
---------              ----------              ---------"

awk -v VM="$var" '{a++;if(a%60){print } else{print ;print VM}}' file

Try this, too, with GNU sed. Beware, it looks to work, but I'm not 100% sure it isn't bugged somehow:

sed -n 'p;G;s/[^\n]//g;t barrier;:barrier;s/^\n\{60\}$//;t insfile;h;d;:insfile;x;r header.txt' <originalfile >newfile

--
Bye

Now, what does that mean (NR being always same)? That might not work only because ! has a higher precedence than % . So the expression !NR%60 is evaluated as follows:
1) Negate the value of NR . This will always will false (0).
2) Evaluate 0%60 which will always return a 0.
So, that expression will always be false. That does not mean NR is the same always. Right? :slight_smile:
The correct conditional expression should be !(NR%60) .

1 Like

It is not. It's a special variable which means 'number of lines read', it changes every time awk loops through a line.

Thanks elixir_sinari and Corona688,

I was confused between NR and NF and no result from (!NR%60) adds to it..:confused:
Just assumed that nature of both the variables is the same..:o
As elixir_sinari explained got it clear..
I should have tried it once..:wall:

Thanks:)