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 pathunkathunk 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
pathunkathunk pathunkathunk is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 May 2017, 5:45 PM EDT
Posts: 57
Thanks: 23
Thanked 0 Times in 0 Posts
Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix.

I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice.

Files:

Code:
cat prefixes.txt 
N2_A
N2_O

cat N2_A_ko.txt 
K02234  6.24
K02588  14.971
K02588|unclassified     14.971

cat N2_O_ko.txt 
K02588  2.647
K02588|unclassified     2.647
K02233 3.45

Here's what I've tried:

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

But, again, instead of printing the prefix as the first column, it prints the first line of the file.

Code:
cat N2_A_nifh.txt 
K02588  14.971 K02588   14.971 

cat N2_O_nifh.txt 
K02588  2.647 K02588    2.647