Take the 2-minute tour ×

I have a file as below:

28 temp 5

I am using the below command for splitting the lines and get the last number in the line.

awk -F"temp" '{print $NF}' temp3

the ouput i got is :

> awk -F"temp" '{print $NF}' temp3
28 temp 5

Surprisingly if i use nawk i am getting the expected output.

> nawk -F"temp" '{print $NF}' temp3

May i know the reason why? Is awk not supporting the string mentioned as a separator?

share|improve this question
If the last number in the line is all that you want sed 's/.*temp *//' file might be a better choice. –  potong Aug 13 '12 at 20:01
I want to explore the behaviour of awk here..i know that this can be done by using other tools like sed and perl. –  Vijay Aug 14 '12 at 6:10
add comment

1 Answer

up vote 2 down vote accepted

Indeed Solaris awk only considers a single character. I'd say it's probably due to tradition, and exactly the reason why nawk is shipped, as well.

The -F switch is really special: it's taking the first character of your quoted string, and discarding the rest, so the t remains --- which stands for "look for tab as field separator".

share|improve this answer
Then if thats the case why it is not considering atleast t as the field separator and printing emp3 as the $NF for the first line? –  Vijay Aug 13 '12 at 13:52
@ShiDoiSi: $NF signifies the contents of the last field on the line. You are correct that AWK doesn't use dollar signs for variables, it uses them for field numbers. NF is the variable that contains the number of the last field. –  Dennis Williamson Aug 13 '12 at 23:42
@DennisWilliamson Right, I didn't pay close attention to his intended output, I thought he wanted to print the number of fields. –  ShiDoiSi Aug 14 '12 at 7:14
add comment

Your Answer


