problem with awk

Dave Ulrick d-ulrick at comcast.net
Tue Jun 1 18:39:27 UTC 2010


On Tue, 1 Jun 2010, Adel ESSAFI wrote:

> Hello,
> I have a "little" proble with awk
>  here I have a file which contain data like this
> 
> 
> 101663.dat
> 1 122837.920343696
> 1 121875.899726134
> 1 8011.13164749145
> 1 24955.1102952732
> 
> 
> when I execute
> 
> awk    'BEGIN { }
>       echo $2
>       END   { print "Fin" }
> ' testclean
> 
> 
> I got this outpout
> 
> 1 122837.920343696
> 1 121875.899726134
> 1 8011.13164749145
> 1 24955.1102952732
> 
> while I am expecting to get
> 
> 122837.920343696
> 121875.899726134
> 8011.13164749145
> 24955.1102952732
> 
> without 1 at the beginning of the line. Can you help please.

Looks like a field separator (FS) problem.  The field separator is used by 
'awk' to divide a line into fields.  The default 'awk' field separator is 
"\t" (tab) but your fields are separated by spaces.  Try adding this 
inside the BEGIN {} block:


FS = " ";


This would give you a script like this:


awk    'BEGIN { FS = " "; }
      echo $2
      END   { print "Fin" }
' testclean

Alternatively, you may specify the field separator as an 'awk' option:

awk -F " " '<your script>' testclean

Dave
-- 
Dave Ulrick
Email: d-ulrick at comcast.net


More information about the users mailing list