🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.misc
Article #2015

Re: Operate on two files at the same time in awk

#2015
From: Barbara Vaughan
Date: Fri, 01 Oct 1993 20:41
55 lines
3174 bytes
In article <1993Oct1.154830.29504@bmerh85.bnr.ca>, mnarayan@bmerha67.bnr.ca (Brian Larmour) writes:
|> In article <1993Sep30.171928.6462@bmerh85.bnr.ca>,
|> Vilok Kusumakar <vilok@bnr.ca> wrote:
|> >I have a problem for awk experts:
|> >
|> >I have two text files file1 and file2.
|> >
|> >I want to look for presence of string at $1(1st column) in file1 in 
|> >file2. And print out all the lines in file1 whose $1 is not present 
|> >in any line in file2.
|> >
|> 
|> 
|> Have you tried using the "join command". "man join" should help.
|> 
I don't think this is what he wants to do.  Here is a little piece of a
rather long awk program that does something similar to what I think is 
being requested.  In my example, I have a file containing the names of
the 50 U.S. states, plus D.C.  I have another bibliographic file where
the "place of publication" is given.  The last field of the "place" is 
either a country (non-US) or a state (U.S).  I need to produce a field
containing the "country of publication".  To do this, since country is 
not given for U.S. publications, I have to compare the "place" field 
with the file containing the names of the states.  (Yes, I know Georgia
is both a state and a country, but the country is called Republic of 
Georgia in this file.)

BEGIN{
for (i = 1; i <= 51; i++){
  getline state[i] < "states"
  }
}
 { place = $1
   country = place
   for (ii  = 1; ii <= 51; ii++){ 
     if (place == state[ii]){
       country = "United States"
       break
       }
     }
   }

In my example the file "states" is the equivalent of file2 in the request.
I read the entire file2 into an array in the BEGIN section.  I've defined
"place" to be $1 to make it more similar to the first column of file1
in the request.  (In my real application it's more complicated than that.)

I wouldn't want to do this if file2 were very large;  I actually sorted
file2 to put it in the order of expected frequency of appearance in file1,
using sort|uniq -c|sort obtained from a large sample of a typical file1. 
Then, I break out of the loop as soon as I get a hit.

I hope this helps.  I don't think there's any better way to do it with awk.

Barbara Vaughan
Technical Details
Message-ID: <1993Oct1.204112.6554@Princeton.EDU>
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!howland.reston.ans.net!spool.mu.edu!olivea!news.bu.edu!att!princeton!sheps!bvaughan
References: <1993Sep30.171928.6462@bmerh85.bnr.ca> <1993Oct1.154830.29504@bmerh85.bnr.ca>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8c9b,be6d506f16d4e9f8
X-Google-Attributes: gidf8c9b,public
X-Google-Thread: f72b0,be6d506f16d4e9f8
X-Google-Attributes: gidf72b0,public
X-Google-Thread: 14b500,be6d506f16d4e9f8
X-Google-Attributes: gid14b500,public
X-Google-ArrivalTime: 1993-10-02 00:27:52 PST
Newsgroups: comp.unix.misc,comp.unix.questions,bnr.unix
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!howland.reston.ans.net!spool.mu.edu!olivea!news.bu.edu!att!princeton!sheps!bvaughan
From: bvaughan@sheps.Princeton.EDU (Barbara Vaughan)
Subject: Re: Operate on two files at the same time in awk
Message-ID: <1993Oct1.204112.6554@Princeton.EDU>
Originator: news@nimaster
Sender: bvaughan@sheps (Barbara Vaughan)
Nntp-Posting-Host: sheps.princeton.edu
Organization: Princeton University
References: <1993Sep30.171928.6462@bmerh85.bnr.ca> <1993Oct1.154830.29504@bmerh85.bnr.ca>
Date: Fri, 1 Oct 1993 20:41:12 GMT
Lines: 55