I want to print the second-to-last column or field in awk. The number of fields is the NF variable. I know that I should be able to use $NF, but I'm not sure how it can be used.
And this does not seem to work:
awk ' { print ( $NF-- ) } ' awk '{print $(NF-1)}' Should work
awk -F '.' '{print $(NF-2)}'$(..) invokes a command in a subshell depending on which shell you're using. You can work around this by using $ (NF-1) instead of $(NF-1).$(…) is just a way to grouping operations inside awk that yields an outcome that could be evaluated to a certain field number. you can do $( atan2(0, -1) ) if you like - it'll simply become $(3.14159…) which is then auto truncated to $3. gawk and nawk are even willing to accept +/- nan as a valid NFSmall addition to Chris Kannon' accepted answer: only print if there actually is a second last column.
( echo | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 2 | awk 'NF && NF-1 { print ( $(NF-1) ) }' echo 1 2 3 | awk 'NF && NF-1 { print ( $(NF-1) ) }' ) It's simplest:
awk '{print $--NF}' The reason the original $NF-- didn't work is because the expression is evaluated before the decrement, whereas my prefix decrement is performed before evaluation.
int x = ++i int x = i++, prefix means increment first; postfix means increment later (assignment first).awk 'NF>1{print $--NF}'.You weren't far from the result! This does it:
awk '{NF--; print $NF}' file This decrements the number of fields in one, so that $NF contains the former penultimate.
Let's generate some numbers and print them on groups of 5:
$ seq 12 | xargs -n5 1 2 3 4 5 6 7 8 9 10 11 12 Let's print the penultimate on each line:
$ seq 12 | xargs -n5 | awk '{NF--; print $NF}' 4 9 11 Perl solution similar to Chris Kannon's awk solution:
perl -lane 'print $F[$#F-1]' file These command-line options are used:
n loop around every line of the input file, do not automatically print every line
l removes newlines before processing, and adds them back in afterwards
a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace
e execute the perl code
The @F autosplit array starts at index [0] while awk fields start with $1.
$#F is the number of elements in @F
$F[-2]If you have many columns and want to print all but not the three cloumns in the last, then this might help
awk '{ $NF="";$(NF-1)="";$(NF-2)="" ; print $0 }'
mawk '3 < NF && NF -= 3' Sat Jul 9 12:30:20 EDT 2022 Sat Jul 9
NFis the last field index,$NFis the value of the last field