AWK..multiple input files

AWK..multiple input files

Post by Kumiko Teraka » Tue, 08 Nov 1994 11:18:31



Help. This is what I want to do. I have 2 files. One
huge one(MASTERFILE) and one small one(FILE1). They look something
like this...

FILE1

COL1    COL2    COL3

Smith   Joe     50,000
Jones   Bill    29,000
Johnson James   38,000
Abdul   Jabaar  49,000

MASTERFILE

COL1    COL2    COL3

Henry   John    33,000
Curry   Bill    24,000
Smith   Joe     47,000
Doe     John    34,000

For all records in FILE1 I want to check to see if the
name exists in MASTERFILE and if so, show me the
difference of the COLUMN3(COL3)  values.
Simple enough. But what is the best way to do this??
Awk?? BSH?? GREP? Any advice would be helpful.
Thanx.

--
----------------------------------------------------------
Jeffrey T. Hazelwood
580 Commonwealth Avenue, Apt. 34
Boston, MA 02215
(617) 262-2346
----------------------------------------------------------

 
 
 

AWK..multiple input files

Post by Heiner Stev » Thu, 10 Nov 1994 03:09:51


[...]
|> FILE1
|>
|> COL1      COL2    COL3
|>
|> Smith     Joe     50,000
|> Jones     Bill    29,000
|> Johnson   James   38,000
|> Abdul     Jabaar  49,000
|>
|>
|> MASTERFILE
|>
|> COL1      COL2    COL3
|>
|> Henry     John    33,000
|> Curry     Bill    24,000
|> Smith     Joe     47,000
|> Doe       John    34,000
|>
|>
|> For all records in FILE1 I want to check to see if the
|> name exists in MASTERFILE and if so, show me the
|> difference of the COLUMN3(COL3)  values.
|> Simple enough. But what is the best way to do this??
|> Awk?? BSH?? GREP? Any advice would be helpful.
|> Thanx.

You can use the following awk-program:

Quote:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>(START)<<<<<<<<<<<<<<<<

# compare - compare column 3 of MASTERFILE and FILE1

nawk '
    BEGIN {
        # read all master records
        while ( getline < "MASTERFILE" )
            Salaries [$1 " " $2] = $3
        close ("masterfile")
        print "Name", "MASTERFILE", "FILE1", "DIFF"
    }
    {                                   # process each line
        Name = $1 " " $2
        if ( Salaries [Name] > 0 )
            print Name, Salaries [Name], $3, Salaries [Name]-$3
    }
' FILE1

Quote:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>(END)<<<<<<<<<<<

You need the new awk "nawk". If your awk cannot compute
with numbers containing a comma "," (like mine) you can
remove the commas from the files with the following lines:

        for i in MASTERFILE FILE1
        do
            tr -d ',' < $i > gnuelpf && mv gnuelpf $i
        done

Hope it helps

Heiner

 
 
 

1. awk: best way to handle multiple files with awk ?

Hello *,

what's the best way to handle multiple files in awk ?
The most simple problems I have is with two files:

file1 contains search patterns (in $3) which I want to
match with $2 of file2.

How to manage this ?

I'm thinking of a solution like

{ pattern[count++] = $3 }  < file1

{ for (i = 0; i < count; i++)
        if ( $2 == pattern[i] )
                print
                }          < file2

What's the best way to combine this in a single
awk program ?

Thanks in advance, bye

Stephan

2. SVGALIB and a serial mouse

3. Extracting multiple lines OR deleting multiple lines from a file using AWK

4. Network Audio Server -- PCM error

5. Redirecting input to a file - echo the input to screen/file?

6. promiscuous mode

7. help on awk and ASCII file input

8. SUN server switches

9. Ses/Awk Search/Replace via text input file

10. awk - get name of input file

11. awk when NF varies along input file

12. Input Multiple files to a Script

13. SAR processing multiple input files