Append header with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Append header with awk
# 1  
Old 05-09-2018
Append header with awk

I have to append the header (ie "START OF LINE") to a file only if there is data in it.
Below command is showing header though there is no data in a file, can you suggest how to correct..
Code:
awk '{NR!=0} BEGIN{print "START OF LINE"}; {print}' file

# 2  
Old 05-09-2018
Why not
Code:
awk 'NR == 1 {print "START OF LINE"} 1' file

?

EDIT: or
Code:
[ -s "$FILE" ] && { echo "START OF LINE"; cat "$FILE"; }

This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-09-2018
RudiC already told you how to do it but you might profit from understanding why your code did not work:

Quote:
Originally Posted by JSKOBS
Below command is showing header w there is no data in a file, can you suggest how to correct..
Code:
awk '{NR!=0} BEGIN{print "START OF LINE"}; {print}' file

The reason why this doesn't work is that "NR" is only updated when records are read. Otherwise awk would have to read the whole file to determine how many records there are in it before it even begins to process it, no?

The BEGIN-part of awk is executed (aas the name implies) before any part of the input file is read and therefore "NR", at the time when this is executed, is always 0.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 4  
Old 05-09-2018
Hello JSKBOS,

Following may also help you on same.
Code:
awk '{printf("%s%s\n",FNR==1?"START OF LINE":"",$0)'   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts Need help... I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp e.g. Header row1 row2 row3 Trailer The script should read all the files under... (3 Replies)
Discussion started by: phani333
3 Replies

2. UNIX for Dummies Questions & Answers

Entering a newline into a header in awk

I want to create a header with awk like this: gawk 'BEGIN {print "List of Events"} Desired output: List of Events Tennis Baseball But I am at a loss on how to do this. I can make a list like this: List of Events Tennis Baseball But I can't get a space to appear. I have... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Is there a way to append both at header and footer of a file

currently I've a file Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('024', '024', 2, sysdate); Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('032', '032', 2, sysdate); ........ is it... (3 Replies)
Discussion started by: jediwannabe
3 Replies

4. Shell Programming and Scripting

[Solved] Append an header to a tab delimited file

Dear All, I would like to find an automatic way to add a given code which belong to a class at the end of the column , for example this is my input file: 0610009O20Rik V$VMYB_01 310 (+) 1 0.971 v-Myb V$EVI1_04 782 (-) 0.763 0.834 Evi-1 V$ELK1_02 1966 (-) 1 0.984 Elk-1... (4 Replies)
Discussion started by: paolo.kunder
4 Replies

5. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

6. UNIX for Dummies Questions & Answers

pull date from header and append to all records

I did some searches, but couldn't really find what I'm looking for. I have a file formatted as below: BOF ABC CO - XYZ COMM DATA OF 07/05/2011 EBA00000001 sdfa rtyus uyml EBB00000001 54682 984w3 EBA00000002 mkiyuasdf 98234 I want to pull the date from the header record and add it... (4 Replies)
Discussion started by: keeferb
4 Replies

7. Shell Programming and Scripting

Append Header from File 1 to File 2

Hi All, I am relatively new to scripting and am stuck on this problem, having tried many combinations of sed and awk. I am generating XML files ('File 2' - 20101214_1800_111_CODE.XML) which contain XML from line 1 starting with <REPORT> I need to insert a header for each file from a... (2 Replies)
Discussion started by: ArtVandelay71
2 Replies

8. Shell Programming and Scripting

Append Header in CSV file

Hi, I create a csv file and the output looks like below Arun,E001 Sathish,E003 Now i need to include the below header and the output should like below Name,Number Arun,E001 Sathish,E003 Please guide me. Thanks (4 Replies)
Discussion started by: Sekar1
4 Replies

9. Shell Programming and Scripting

Append Spaces At end of each line Leaving Header and Footer

How to append constant No of spaces suppose 52 at end of each line in a file (xyz) excluding first and last line. Please Help me out for the same. (1 Reply)
Discussion started by: deepam
1 Replies

10. UNIX for Dummies Questions & Answers

Append Header and Trailer

Hi everyone, I am new to Unix programming. My inquries is:- a) How to add a Header and Trailer in the set of data b) Include a number count of the data in the trailer The set of data only contained the information of 'Customer's Name' and 'Account Number'. I would like to add the Header... (2 Replies)
Discussion started by: balzzz
2 Replies
Login or Register to Ask a Question