5

I have a file as below:

10temp3
20/temp4
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
10temp3
20/temp4
28 temp 5

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

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

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

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 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 06:10

1 Answers1

4

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".

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
  • 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
  • 1
    @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. – Volker Stolz Aug 14 '12 at 07:14