Unix/Linux 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


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-16-2017   -   Original Discussion by pathunkathunk
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

Sponsored Links
    #2  
Old Unix and Linux 05-16-2017   -   Original Discussion by pathunkathunk
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 November 2017, 10:23 PM EST
Location: San Jose, CA, USA
Posts: 10,660
Thanks: 572
Thanked 3,734 Times in 3,186 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)
Sponsored Links
    #3  
Old Unix and Linux 05-17-2017   -   Original Discussion by pathunkathunk
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 19 November 2017, 9:36 PM EST
Location: Simplicity
Posts: 3,824
Thanks: 318
Thanked 1,281 Times in 1,159 Posts
Can be elegantly done with shell builtins

Code:
while read file
do 
  while read id w wr
  do
    case $id in
    K02588 )
      printf "%s\t%s\t%s\n" "$file" "$id" "$w"
      break
    ;;
    esac
  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 04:54 AM..
    #4  
Old Unix and Linux 05-17-2017   -   Original Discussion by pathunkathunk
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 17 November 2017, 12:40 AM EST
Posts: 179
Thanks: 26
Thanked 38 Times in 33 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
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:57 PM
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 11:59 AM
Take input from read and place it a string in another file svetoslav_sj Shell Programming and Scripting 9 01-26-2012 01:16 PM
Print only Filename based on given String on file name akb2010 Shell Programming and Scripting 5 01-21-2010 01:51 AM
Howto Print File Path or Print the Filename overkill Shell Programming and Scripting 1 10-09-2009 02:00 AM



All times are GMT -4. The time now is 11:44 PM.