Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk last argument before NF?

Tags:

awk

I tried to print the argument before $NF. However $NF-- doesn't do the trick. What is the solution to this? Problem is, I dono how many args I get, so I always need $NF and the arg before.

Kind Regards

Juergen

host -t ptr 1.1.1.1 | awk '/pointer/ {num=split($0,a, "."); print a[num-2] "." a[num-1] ;}'

foo.tld
like image 586
Juergen Avatar asked Mar 16 '12 21:03

Juergen


People also ask

How do I get the last element in awk?

$NF as with any $fieldnumber usage in awk prints the value of the data element stored in the last field on every line. So if the first line in a file has 4 fields and the second line has 3, NF for each line is 4 and 3 respectively, and $NF is the respective values in $4 on the first line and $3 on the second line.

How can I see the last line in awk?

If you make awk to spit out its output you can pipe it to the next processor. If you use getline, and in particular within a loop, you might not see the end. getline should be used only for a line and an eventual dependency on the next line.

What is NF in awk?

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. No matter how many fields there are, the last field in a record can be represented by $NF .


1 Answers

You can use $(NF - 1) to get the next to last field. So in your case:

awk '{print $(NF - 1), $NF}'
like image 120
Eduardo Ivanec Avatar answered Oct 14 '22 18:10

Eduardo Ivanec