Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store file header information into variable using awk command? 2

Status
Not open for further replies.

rchowd2

Technical User
Jan 4, 2005
8
US
I have been trying to retrieve only the file header information from the file using AWK command and store the header string into the variable and then use the variable to append to the log file. Any advise on how can I store the file header information which starts with the string "HEADER" and store the whole line string into variable and append it to the log file.

I tried to use the following command in order to print the header information, unfortunately it's printing the every line in the file.

Awk '{print substr($2,1,length($2))}' file1.dat


Example:
File 1 contains the following Header String:


HEADER 2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01 $


and I want to store the following string to the variable

"HEADER 2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01"

Then append the variable to the log file.

Thanks in advacne for your help!

Russel
 
hdr=`awk '/HEADER/{print $2;exit}' file1.dat`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PHV! I tried to execute the script and it only prints the following info not the complete header string:

2/16/0515.03.30TIO01676


I need to store the complete string:

2/16/0515.03.30TIO01676 CMS.TST.IWUNV676.IO01
 
Code:
hdr=`awk '/HEADER/{NF--;$1=$1;print;exit}' file1.dat`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
hdr=`awk '/HEADER/{print substr($0,7);exit}' file1.dat`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
well actually it should be like this:
Code:
hdr=`awk '/^HEADER/{$1="";NF--;gsub(/^[ ]*/,"");print;exit}' file1.dat`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top