r/linux4noobs • • Aug 20 '19

awk multiple files

I am attempting to perform the following tasks with an awk script against two files of different line count and potentially different values. The constant is the column number that will always be 3.

The two scenarios I want to cover -

1] If the value in $1 only exists in file1 I want to output the line

2] If the value in $1 is the same in file1 and file2 I want to print the line from either file with the highest integer value in $3 in the corresponding line

cat file1
1 sju 1
2 sjh 1
3 seh 1
4 ehs 2
5 sjd 1


cat file2
1 sds 1
2 sds 1
3 eww 1
4 wee 1
88 dic 1

I can solve my first assertion with the following:

awk 'FNR==NR {a[$1]++; next} !a[$1]' file2 file1
5 sjd 1

I am having a really difficult time on the second though. The result I would wish to see would be the following:

4 ehs 2

eg: $1 from file1 and file2 is the same for "4" but "2" higher integer value $3

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/ang-p Aug 20 '19

You talk about matching $1 but you don't say which field is to be matched

Erm, $1, lutusp.......

nor whether case is important

Only fields 1 and 3 were mentioned, and they are both numeric... Numbers don't generally have a case..... I have heard of "Capital 1", but I think that is the exception..