Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old 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.


cat prefixes.txt 

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:

while read 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.

cat N2_A_nifh.txt 
K02588  14.971 K02588   14.971 

cat N2_O_nifh.txt 
K02588  2.647 K02588    2.647

Sponsored Links
Old 05-16-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 18 October 2017, 5:21 AM EDT
Location: San Jose, CA, USA
Posts: 10,536
Thanks: 548
Thanked 3,689 Times in 3,147 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:

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)
Sponsored Links
Old 05-17-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Join Date: May 2012
Last Activity: 18 October 2017, 2:55 AM EDT
Location: Simplicity
Posts: 3,778
Thanks: 311
Thanked 1,265 Times in 1,144 Posts
Can be elegantly done with shell builtins

while read file
  while read id w wr
    case $id in
    K02588 )
      printf "%s\t%s\t%s\n" "$file" "$id" "$w"
  done <"$file"_ko.txt >"$file"_nifh.txt
done < prefixes.txt

EDIT: just seeing it is not always found in the first line, therefore added another while loop (and actually the previous awk solution does this loop)

Last edited by MadeInGermany; 05-17-2017 at 03:54 AM..
Old 05-17-2017
rovf rovf is offline
Registered User
Join Date: Jun 2011
Last Activity: 12 September 2017, 2:10 AM EDT
Posts: 171
Thanks: 24
Thanked 31 Times in 27 Posts
You didn't say which awk you are using. If your awk is linked to nawk or gawk, there is also an alternative to the solution posted by Don Cragun, in that you could export your shell variable (so that it becomes an environment variable) and use awk's built-in ENVIRON array to access it. If you follow this route, I strongly recommend to write the variable in all-uppercase.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read each line from input file, assign variables, and echo to output file? Scottie1954 Shell Programming and Scripting 6 03-22-2014 11:57 AM
Read input files and merge them in given order and write them to input one param or one file hyd1234 Shell Programming and Scripting 4 02-11-2014 10:59 AM
Take input from read and place it a string in another file svetoslav_sj Shell Programming and Scripting 9 01-26-2012 12:16 PM
Print only Filename based on given String on file name akb2010 Shell Programming and Scripting 5 01-21-2010 12:51 AM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 01:00 AM

All times are GMT -4. The time now is 09:07 AM.