[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] gawk not working properly on the first line
From: |
Davide Brini |
Subject: |
Re: [bug-gawk] gawk not working properly on the first line |
Date: |
Mon, 11 Aug 2014 23:01:15 +0200 |
On Tue, 12 Aug 2014 01:55:42 +1000, Miriam English <address@hidden>
wrote:
> Yesterday I was trying to process a small table of tab-separated values
> to select out 3 of the 6 columns and print them formatted. I used this
> piece of awk along with some sed bits and pieces:
> awk '{FS="\t" ; print $3 "- " $2 "[" $5 "]"}'
>
> Here is a sample table:
> Play 01 A Filbert is a Nut Rick Raphael Etext
> Steve Mattingly Play 02 Ask a Foolish Question
> Robert Sheckley Etext Bellona Times Play 03 The
> Beast in the Void Paul W. Fairman Etext James
> Rogers Play 04 The Burning Bridge Poul William Anderson
> Etext Mark Nelson Play 05 From an Amber Block
> Tom Curry Etext Lars Rolander
>
> It is supposed to produce:
> Rick Raphael - A Filbert is a Nut [Steve Mattingly]
> Robert Sheckley - Ask a Foolish Question [Bellona Times]
> Paul W Fairman - The Beast in the Void [James Rogers]
> Poul William Anderson - The Burning Bridge [Mark Nelson]
> Tom Curry - From an Amber Block [Lars Rolander]
>
> It worked perfectly on every line of the table except the first line.
>
> A- 01[is]
> Robert Sheckley - Ask a Foolish Question [Bellona Times]
> Paul W. Fairman - The Beast in the Void [James Rogers]
> Poul William Anderson - The Burning Bridge [Mark Nelson]
> Tom Curry - From an Amber Block [Lars Rolander]
>
> When I inserted a blank line before the first line it worked on that
> line too, so it wasn't a problem with the line itself. It looks to me
> like awk is not setting the field separator to TAB until after
> processing the first line.
>
> Is this a genuine bug or am I misusing awk somehow?
You are supposed to set FS before awk reads and tries to parse the line; in
this case, that means setting it in the BEGIN block or on the command line
with -v.
This is documented in the gawk manual, see
https://www.gnu.org/software/gawk/manual/gawk.html#Field-Splitting-Summary
the box titled "changing FS does not affect the fields".
--
D.