bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with changing FS


From: Stephane Chazelas
Subject: Re: Problem with changing FS
Date: Sat, 15 Sep 2007 15:18:33 +0100
User-agent: Mutt/1.5.16 (2007-06-11)

On Wed, Sep 12, 2007 at 11:40:25AM -0400, Scott McNeilly wrote:
> Here are two scripts, one uses awk on tru64 Unix 5.1b, and the other uses
> gawk on the same computer.
> 
> The first one, using awk as supplied with the tru64 OS, works.  The second,
> using gawk 3.1.5, doesn't work as expected.
[...]
> line='dyn~11-SEP-07~isir_load_date'
[...]
> var=`echo $line | awk '{{ FS = "~"} { print $2}}'`
> echo 'var=' $var
[...]
> var= 11-SEP-07
[...]
> var=`echo $line | gawk '{{ FS = "~"} { print $2}}'`
> echo 'var=' $var
[...]
> var=
[...]

It's your first awk that is incorrect in that case. awk
(according to the POSIX or Unix specs) is supposed to split $0
into $1, $2... with the value the FS parameter had at the time
the record was read. Here, you modify FS after the first record
was read, so the new value of FS will only be used for the
second and following records.

So

echo a:b | awk '{FS=":"; print $1}'

should output "a:b", not "a".

If you want $0 to be split with the new value of FS, you should
assign a new value to it.

like:

echo a:b | awk '{FS=":"; $0 = $0; print $1}'

But best is to assign FS before any record is read like in:

echo a:b | awk -F: '{print $1}'

or

echo a:b | awk '
  BEGIN { FS = ":" }
  { print $1 }'

-- 
Stéphane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]