🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #1768

Re: How to get AWK to output 2 fields at once

#1768
From: Don Bolton
Date: Tue, 13 Nov 1990 20:43
67 lines
2654 bytes
In article <46366@sequent.UUCP> lugnut@sequent.UUCP (Don Bolton) Will live
to regret writing :-)

>In article <1990Oct29.171816.7459@mrspoc.Transact.COM> itkin@guinan.Transact.COM writes:
>>jak9213@helios.TAMU.EDU (John Kane) writes:
>>
>>>In article <297@twg.bc.ca> bill@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!
>
>except the for statement still loops for EACH argument retrieved by your
>awk statement. SOOOO... try

OK I'm blind, missed the external " "s that you even mentioned. I hate
it when that happens.

So SHOOT ME NOW

<BOOM>

>#Place in a _ to join $1 and $2 so the for loop sees a single arg
>
>for x in `cat file_1 | awk '{print $1"_"$2}'`
>    do
>
>#Using the -F option for awk, remove the joiner for use by grep
>
>	blarg=`echo $x | awk -F_ '{print $1, $2}'`
>        grep $blarg file_2	
>    done
>
>Now you'll get what you want

Must be time for lunch.
certainly out to it anyways :-)
Technical Details
Message-ID: <46373@sequent.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!zephyr.ens.tek.com!tektronix!sequent!lugnut
References: <297@twg.bc.ca> <9622@helios.TAMU.EDU> <1990Oct29.171816.7459@mrspoc.Transact.COM> <46366@sequent.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!zephyr.ens.tek.com!tektronix!sequent!lugnut
From: lugnut@sequent.UUCP (Don Bolton)
Newsgroups: comp.unix.shell
Subject: Re: How to get AWK to output 2 fields at once
Message-ID: <46373@sequent.UUCP>
Date: 13 Nov 90 20:43:19 GMT
References: <297@twg.bc.ca> <9622@helios.TAMU.EDU> <1990Oct29.171816.7459@mrspoc.Transact.COM> <46366@sequent.UUCP>
Reply-To: lugnut@sequent.UUCP (Don Bolton)
Organization: Sequent Computer Systems, Inc
Lines: 67