Article 4VW89 [SOLVED] awk command: only print the longest lines when last field is identical

[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!latest?d=yIl2AUoC8zA latest?i=yV8PlXahdjE:vu_0dmLT2FQ:F7zBnMy latest?i=yV8PlXahdjE:vu_0dmLT2FQ:V_sGLiP latest?d=qj6IDK7rITs latest?i=yV8PlXahdjE:vu_0dmLT2FQ:gIN9vFwyV8PlXahdjE
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments