Unix/Linux Go Back    



Use while loop to read file and use ${file} for both filename input into awk and as string to print

Shell Programming and Scripting




Kindly Note - This is a Single User Post by Forum Member Don Cragun Regarding:
Use while loop to read file and use ${file} for both filename input into awk and as string to print.
Please Follow The Primary Link Above to View the Full Discussion.

   
Old Unix and Linux 05-16-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 21 October 2017, 12:49 AM EDT
Location: San Jose, CA, USA
Posts: 10,553
Thanks: 554
Thanked 3,696 Times in 3,153 Posts
You're close, but shell variables aren't known in an awk script unless you explicitly pass them in. And, in awk $var prints the field named by the field number contained in the awk variable var (and since var hasn't been defined in your awk script it expanded to $0 which is the contents of the current line). Try:

Code:
while read file
do	awk -F'\t' -v var="$file" '/K02588/ {print var,$1,$2; exit}' "${file}_ko.txt" > "${file}_nifh.txt"
done < prefixes.txt

The Following User Says Thank You to Don Cragun For This Useful Post:
pathunkathunk (05-16-2017)