The following command line uses awk
to prepend the last field of each line of file.txt, does a reverse numerical sort, then uses cut
to remove the added field:
awk '{print $NF,$0}' file.txt | sort -nr | cut -f2- -d' '
Almost there. Just put both column references next to each other.
cat logfile | sed 's/|/ /' | awk '{print $1, $8}'
Also note that you don't need cat
here.
sed 's/|/ /' logfile | awk '{print $1, $8}'
Also note you can tell awk
that the column separators is |
, instead of blanks, so you don't need sed
either.
awk -F '|' '{print $1, $8}' logfile
As per suggestions by Caleb, if you want a solution that still outputs the last field, even if there are not exactly eight, you can use $NF
.
awk -F '|' '{print $1, $NF}' logfile
Also, if you want the output to retain the |
separators, instead of using a space, you can specify the output field separators. Unfortunately, it's a bit more clumsy than just using the -F
flag, but here are three approaches.
You can assign the input and output field separators in awk
itself, in the BEGIN block.
awk 'BEGIN {FS = OFS = "|"} {print $1, $8}' logfile
You can assign these variables when calling awk
from the command line, via the -v
flag.
awk -v 'FS=|' -v 'OFS=|' '{print $1, $8}' logfile
or simply:
awk -F '|' '{print $1 "|" $8}' logfile
Best Answer
It can even be done only with
'bash'
, without'sed'
,'awk'
or'perl'
: