Skip to main content
Post Made Community Wiki by don_crissti
added 258 characters in body
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

Save the entire line (using $6 as array index) and in END iterate over the elements of the array:

awk '{z[$6]=$0};END{for (i in z) print z[i]}' logfile

The result won't be sorted though... Don't bother with complex sorting, just runYou could do something like:

awk '{z[$6]=NR" "$0};END{for (i in z) print z[i]}' logfile | sort -k1,1n | cut -f2-
### this space ^ is a literal TAB

which saves the line no. plus the line content to be able to then sort by line no.


Other ways involve a 2nd pass and you'llto get it sorted by date since(since this is a log) but will print duplicate entries if the input contains duplicate lines (entire lines, that is) - e.g. with grep:

awk '{z[$6]=$0};END{for (var in z) print z[var]}' logfile | grep -Fxf- logfile

or do it entirely inonly with awk:

awk 'NR==FNR{z[$6]=$0;next}
FNR==1{for (var in z) y[z[var]]}
$0 in y' logfile logfile

Save the entire line (using $6 as array index) and in END iterate over the elements of the array:

awk '{z[$6]=$0};END{for (i in z) print z[i]}' logfile

The result won't be sorted though... Don't bother with complex sorting, just run a 2nd pass and you'll get it sorted by date since this is a log - e.g. with grep:

awk '{z[$6]=$0};END{for (var in z) print z[var]}' logfile | grep -Fxf- logfile

or do it entirely in awk:

awk 'NR==FNR{z[$6]=$0;next}
FNR==1{for (var in z) y[z[var]]}
$0 in y' logfile logfile

Save the entire line (using $6 as array index) and in END iterate over the elements of the array:

awk '{z[$6]=$0};END{for (i in z) print z[i]}' logfile

The result won't be sorted though... You could do something like:

awk '{z[$6]=NR" "$0};END{for (i in z) print z[i]}' logfile | sort -k1,1n | cut -f2-
### this space ^ is a literal TAB

which saves the line no. plus the line content to be able to then sort by line no.


Other ways involve a 2nd pass to get it sorted by date (since this is a log) but will print duplicate entries if the input contains duplicate lines (entire lines, that is) - e.g. with grep:

awk '{z[$6]=$0};END{for (var in z) print z[var]}' logfile | grep -Fxf- logfile

or only with awk:

awk 'NR==FNR{z[$6]=$0;next}
FNR==1{for (var in z) y[z[var]]}
$0 in y' logfile logfile
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

Save the entire line (using $6 as array index) and in END iterate over the elements of the array:

awk '{z[$6]=$0};END{for (i in z) print z[i]}' logfile

The result won't be sorted though... Don't bother with complex sorting, just run a 2nd pass and you'll get it sorted by date since this is a log - e.g. with grep:

awk '{z[$6]=$0};END{for (var in z) print z[var]}' logfile | grep -Fxf- logfile

or do it entirely in awk:

awk 'NR==FNR{z[$6]=$0;next}
FNR==1{for (var in z) y[z[var]]}
$0 in y' logfile logfile