Visit Our UNIX and Linux User Community

Inserting header after every nth line

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting header after every nth line
# 1  
Old 09-19-2012
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.

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!
# 2  
Old 09-19-2012

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

# 3  
Old 09-19-2012
Try something this..
var="FirstName              LastName                Address
---------              ----------              ---------"

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

Last edited by pamu; 09-19-2012 at 04:41 PM..
# 4  
Old 09-19-2012
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

# 5  
Old 09-19-2012
Originally Posted by pamu
NR is always the same.. so (!NR%60) doesn't work here...
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? Smilie
The correct conditional expression should be !(NR%60).
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 09-19-2012
Originally Posted by pamu
NR is always the same..
It is not. It's a special variable which means 'number of lines read', it changes every time awk loops through a line.
# 7  
Old 09-19-2012
Thanks elixir_sinari and Corona688,

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


Last edited by pamu; 09-20-2012 at 05:04 AM..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Inserting a header with special character

Hi, I am trying to insert header row with a special character delimiter with Unicode u0109 into a file with ‘echo’, header looks like below echo –e “header1\u0109header\u0109header3\u0109header4” It just inserting as it is in the quotes but not the special character, Please suggest if am... (2 Replies)
Discussion started by: oom
2 Replies

3. UNIX for Beginners Questions & Answers

Inserting Header at different position in a file

I would like to hear your directions on how to Insert theses tag </TITLE> and <TEXT> at a given position in 1000 of text files. My Files look like as samplefile1.txt <DOC> <DOCNO>3_September_2012</DOCNO> <TITLE> ... ... ... .... ... .. .. .. ... .. .. .... </TITLE> <TEXT> .... (1 Reply)
Discussion started by: imranrasheedamu
1 Replies

4. Shell Programming and Scripting

Inserting Header to another file

Need your help in appending header(file1 contains header ) to my file2. I am using KSH AIX OS. I know how to do with taking temporary files. cat file1 >temp cat file2 >>temp mv temp file2 Is there way to append directly to a file in ksh. I don't find Sed -i option on my... (10 Replies)
Discussion started by: gvkumar25
10 Replies

5. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Ubuntu

Inserting a header with column number to a 1.6 GB file with special spacing

Hi; I've been searching posts to find a solution to what I'm trying to do, but I've have NOT found anything yet. I have a file (file1) with 300K columns and 1411 rows, the columns don't have a column no. header (No header at all) and I'm trying to fetch the information from specific columns.... (3 Replies)
Discussion started by: sogi
3 Replies

9. Shell Programming and Scripting

Inserting Header and footer

Hi All, I have several txt files i need to enter specific header and footer (both are separate) to all these files how can i do this? plz help.. Regards, Raghav (4 Replies)
Discussion started by: digitalrg
4 Replies

10. Shell Programming and Scripting

Inserting a String in a file header.

Dear all, I have a file created in the name sample.txt in UNIX with header and footer. How to insert a required string (for example "FILE1") in the header part after the file has been created. What kind of command can i use to do the same. Thanks in advance Hari (3 Replies)
Discussion started by: Hari123
3 Replies

Featured Tech Videos