Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to print the last field then the first using $NF [duplicate]

Tags:

awk

I'm not sure I understand the NF variable in awk.

My example file (test) contains one line:

1 2 3 4 5

The following works as expected:

awk '{print $1,$NF}' test
1 5

But the next example does not (i.e. the reverse):

awk '{print $NF,$1}' test
 1

What am I missing here? I can print $NF by itself, or after $1, but not before.

like image 526
Chris Avatar asked Oct 15 '25 12:10

Chris


1 Answers

Since it works fine for me, I am suspecting it could be carriage characters which could cause this issue, if this is the case then try following:

1- Check if carriage characters are present in any Input_file by doing:

cat -v Input_file

2- In case you find any carriage characters in your Input_file then you could remove them by doing following:

tr -d '\r' < Input_file > temp_file  &&  mv temp_file  Input_file
like image 154
RavinderSingh13 Avatar answered Oct 17 '25 14:10

RavinderSingh13