This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes

I have some output from which I want the second-last column displayed. I know I can display the n-th column with awk like this:

... | awk '{print $13}'

However, the number of fields counted from the beginning of the line may vary, only the position counted from the end of the line is stable.

I'm aware that the last field in awk can be referenced via $NF

... | awk '{print $NF}'

but how do I get to the second-last field from there?

in Scripting
by (100)
1 4 11
edit history

Your answer


Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
By submitting this post you agree to our Terms & Conditions.
To avoid this verification in future, please log in or register.

1 Answer

0 votes

The actual variable is NF. $NF just gives you the value of the field at position NF. You can calculate the desired field like this:

... | awk '{i = NF-1; print $i}'

or, even shorter, by using an expression in parentheses:

... | awk '{print $(NF-1)}'
by (100)
1 4 11
edit history