How to get AWK to output 2 fields at once

Engbert Gerrit IJff engbert at cs.vu.nl
Fri Nov 2 02:20:07 AEST 1990


In article <1990Oct29.171816.7459 at mrspoc.Transact.COM>,
	itkin at mrspoc.Transact.COM (Steven M. List) writes:
) jak9213 at helios.TAMU.EDU (John Kane) writes:
) 
) >In article <297 at twg.bc.ca> bill at twg.bc.ca (Bill Irwin) writes:
) >>I  have what initially seemed to be a simple requirement:  get the  first
) >>two fields from each line in file_1, and use them as a search pattern for
) >>GREP to extract matching lines in file_2.  [...]
) 
) >>for x in `cat file_1 | awk '{ print $1 " " $2 }'`
) >>do
) >>        grep "$x" file_2
) >>done
) 
) >>Of course, the GREP routine executed with x having the value of the first
) >>field  of  the  first line of file_1, then with the value of  the  second
) >>field  of  the first line of file_1, then the first field of  the  second
) >>line, .....
) 
) >>Is  there a way to get AWK to output "field_1 field_2" as the value of x,
) >>so  that  this  can be used as the search pattern for GREP,  rather  than
) >>"field_1" "field_2" "field_1" "field_2"?
) 
) >Yep, There is.
) 
) >for x in `cat file_1 | awk '{print "\"" $1 " " $2 "\"")'`
) >do
) >   grep "$x" file_2
) >done
) 
) This seems a bit complicated, doesn't it?  How about:
) 
)     for x in "`cat file_1 | awk '{print $1, $2}'`"
)     do
)         grep "$x" file_2
)     done
) 
) That is, why worry about the backslashes and quotes INSIDE AWK, when you
) can put them outside?  Clean and simple!
) -- 

IMHO you will only get one x containing the whole awk output
and an this will cause the error message
	grep: illegal regular expression: No remembered search string.
However, using fgrep in your simple example works fine,
provided the whole construct is NOT meant to search for
regular expressions.

Bert.



More information about the Comp.unix.shell mailing list