[SOLVED] awk command: only print the longest lines when last field is identical
by masavini from LinuxQuestions.org on (#4VW89)
hi,
i have a large file like this:
Code:$ cat file.txt
123
longest line where 456 is the last field 456
456
0 123
034 123
a b c 456
longest123line 123
very long line containing 456 and 123, but last field is 789i'd like uniq lines by the last field, printing only the longest line for each needle.
in this example the only lines to be printed should be:
Code:longest line where 456 is the last field 456
longest123line 123
very long line containing 456 and 123, but last field is 789i tried and deal with awk associative arrays, but it seems like they can only contain integer values:
Code:awk '!x[$NF] || length($0) > length(x[$NF]) {x[$NF]=$0} END {print x}' file.txt
awk: cmd. line:1: (FILENAME=file.txt FNR=1) fatal: attempt to use array `x' in a scalar contextthanks for your help!


i have a large file like this:
Code:$ cat file.txt
123
longest line where 456 is the last field 456
456
0 123
034 123
a b c 456
longest123line 123
very long line containing 456 and 123, but last field is 789i'd like uniq lines by the last field, printing only the longest line for each needle.
in this example the only lines to be printed should be:
Code:longest line where 456 is the last field 456
longest123line 123
very long line containing 456 and 123, but last field is 789i tried and deal with awk associative arrays, but it seems like they can only contain integer values:
Code:awk '!x[$NF] || length($0) > length(x[$NF]) {x[$NF]=$0} END {print x}' file.txt
awk: cmd. line:1: (FILENAME=file.txt FNR=1) fatal: attempt to use array `x' in a scalar contextthanks for your help!