Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input:

value11,value12,value13
value21,value22,value23

I have successfully done this before using awk with the following code:

 awk 'NR == 1 {FS=","; print $1}' inputfile 

When I try to do this with gawk however, it ignores the field separator. I have tried numerous variations of using BEGIN and rearranging the order but no matter what I do it will either ignore the record number or the field separator. I'm sure there's some little syntax thing that I'm overlooking, but I can't quite seem to figure out what it is.

Can anyone point out what I might be doing incorrectly?

On a side note, when I did it using awk it was on a Unix machine, but right now I'm using gawk on Cygwin on my Windows machine.

Thanks in advance.

How about this ?

gawk -F"," 'NR == 1 {print $1}' inputfile
1 Like

You need to specify the field separator before you start reading the first line, not during..
e.g.

awk -F, '{print $1; exit}' infile
awk '{print $1; exit}' FS=, infile
awk -v FS=, '{print $1; exit}'
awk 'BEGIN{FS=","}{print $1; exit}' infile
1 Like

You are setting the FS variable inside an action statement when awk/gawk has already read in the first line and decided on the fields based on the current value of FS (which was none). Hence, $1 will always be the full record/line read.

You can see this difference if you omit the pattern...

 awk '{FS=","; print $1}' inputfile

Output:

value11,value12,value13
value21

As you can make out,

1) first line had no FS set before the line was read...so $1 was $0..

2) FS was set to a comma by the previous action so, $1 was what you wanted...

1 Like

Thanks. All of your suggestions worked (and saved me a lot of headache).