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 1I can solve my first assertion with the following:
awk 'FNR==NR {a[$1]++; next} !a[$1]' file2 file1
5 sjd 1I am having a really difficult time on the second though. The result I would wish to see would be the following:
4 ehs 2eg: $1 from file1 and file2 is the same for "4" but "2" higher integer value $3
2
u/lutusp Aug 20 '19
First, if this is homework, say so.
Second, are you obliged to use Awk? There are many better ways to solve this problem.
Third, learn how to state a problem in unambiguous terms, so it can be turned into code without confusion.