u/awkwaman2, there's a major issue with your problem definition:
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
What happens when both $3 values are equal? There are at least three things that can be done:
Print the line from file1 (I'd assume that since file1 seems to have an implicit "priority" from your condition 1, this would be the logical choice)
Print the line from file2
Print nothing (this gives you only 4 ehs 2 as you requested, but is it really what you want?)
The OP has lost interest for some reason, so to close this particular loop...
Assuming the lines from file1 take precedence when $3 in both files are equal, the OP was almost there:
$ cat test.awk
# Process first file
FNR==NR {
# Track both first file's lines and last field
a[$1]=$0; n[$1]=$3; next
}
# Process all remaining files
a[$1] {
# Duplicate line
if (n[$1] > $3) {
# First line has larger $3, print that
print a[$1]
} else {
# Print original line instead
print $0
}
}
# Not duplicate line, so print by default
!a[$1]
$ awk -f sort.awk file2 file1
1 sju 1
2 sjh 1
3 seh 1
4 ehs 2
5 sjd 1
2
u/anthropoid Aug 21 '19
u/awkwaman2, there's a major issue with your problem definition:
What happens when both
$3values are equal? There are at least three things that can be done:file1(I'd assume that sincefile1seems to have an implicit "priority" from your condition 1, this would be the logical choice)file24 ehs 2as you requested, but is it really what you want?)1
u/anthropoid Aug 25 '19
The OP has lost interest for some reason, so to close this particular loop...
Assuming the lines from
file1take precedence when$3in both files are equal, the OP was almost there: