-1

How can I print the second Field, counting from the end of the fields, dynamically?

input

4 6 5 6 4

4 5 6

7 8 9 6 3

4 5 6

I would like to print the second column, numbered from the last record (dynamically)

The desired result is:

6
5
6
5

I tried -$2 that does not work.

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
Alex
  • 347
  • 1
  • 10
  • 1
    See: [Print second last column/field in awk](https://stackoverflow.com/q/2096490/3776858) – Cyrus Mar 28 '20 at 15:34
  • 2
    Does this answer your question? [Print second last column/field in awk](https://stackoverflow.com/questions/2096490/print-second-last-column-field-in-awk) – Daemon Painter Mar 28 '20 at 19:01

1 Answers1

2

Could you please try following, written and tested with shown samples.

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

Where NF is out of the box(default) variable of awk and represents the total number of fields in a line. So what I have done is after $ I have put NF-1 means telling to print 2nd last field here.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93