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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Search Values from a Second File 4

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I want to thanks the many out there that give so much to helping others learning about “awking”. Your help has really made a difference in my being able to tackle data extractions and formatting.
I am extracting data from a text file. The text file has thousands of line and up to 25 columns. What the script searches on for column 1 I want to bring in from a second file. Here is the script that I am now using:

if( $1== “<search string>” && $2 ~ /abc/)
print $1, $2, $4
}

input file:
12345678 abcde 567klm 6789
45678123 abcde 5694kn 5678
12345678 abcde 567klm 4589
22345678 abcde 786klm 3426
22345678 abcde 567klm 4573
45678123 abcde 5694kn 5678

2nd file (for col 1 search string):
12345678
22345678

desired output:

12345678 abcde 6789
12345678 abcde 4589
22345678 abcde 3426
22345678 abcde 4573

The values for the search on col 1 has quantities up to 30. I want to read in these values so that I do not have to build the script 30 times. Thanks.
 
Try and adapt the following script :

Code:
awk -v SearchFile=/path/to/search_file '
BEGIN {
   while ((getline < SearchFile)>0)
     SEARCH[$1] = "";
   close(SearchFile);
}
$1 in SEARCH && $2 ~ /abc/ {
   print $1,$2,$4;
}
' /path/to/input_file


Jean Pierre.
 
Make awk script named "s.awk":
Code:
FNR==NR {sought[$0]++; next}
$1 in sought && $2 ~ /abc/ {print $1,$2,$4}
Invoke with [tt]awk -f s.awk searchKeysFile dataFile[/tt]
 
I like the 'FNR==NR' pattern...
A star for you futurlet.

Jean Pierre.
 
Thanks, Jean. You're very generous and objective. Since my
brainpower is quite limited, I look for the simplest way
to solve problems.
 
futurelet,
Thanks for your help. Could you offer some explanation for how this solution works? As a novice awk user I have not used the "awk -f s.awk searchKeysFile dataFile" format. Thanks again.
thunderkid
You get a star!
 
awk -f s.awk searchKeysFile dataFile
launch an awk program named s.awk with two input files searchKeysFile and dataFile.
FNR==NR
while reading 1st file (i.e. current line number still equal total number of lines read so far)
{sought[$0]++; next}
load associative array sought with key=current record;
read the next record and start the main input loop again ( so the rest of the program is for the 2nd input file only)
$1 in sought && $2 ~ /abc/
if 1st field is a key in the sought array and 2nd field contains the string "abc"
{print $1,$2,$4}
print the 1st,2nd and 4th fields

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
A star for aigles for starting me in the right direction and for PHV for his lucid explanation.
 
futurelet, thanks for the feedback, but which star ?...

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top