Artificial intelligent assistant

Print the last-but-one field How to print the word before the last word in line (with ksh or awk or sed or perl one liner) Example 1: echo one two three will print "two" Example 2: echo 1 2 3 4 5 6 will print "5"

With awk:


awk '{ print $(NF-1) }'


`NF` is the number of fields -- all that happens here is that one is subtracted from the total field length to get the penultimate field.

With perl:


perl -lane 'print $F[-2]'


An array containing the fields is created as `@F` (that's what `-a` does), and we get the value of the second last field (with index `-2`).

Using sed is slightly less palatable, since it doesn't have any concept of fields. I'd recommend using one of the above, instead.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 1bf5763cea5ff5bcd0d5816833107cad