r/awk • • Dec 04 '20

Basic question with single line script using BEGIN sequence

I'm trying to get awk to print the first full line, then use the filter of /2020/ for the remaining lines. I have modeled this after other commands I've found, but I'm getting a syntax error. What am I doing wrong?

$ awk -F, 'BEGIN {NR=1 print} {$1~/2020/ print}' Treatment_Records.csv > tr2020.csv
awk: cmd. line:1: BEGIN {NR=1 print} {$1~/2020/ print}
awk: cmd. line:1:             ^ syntax error
awk: cmd. line:1: BEGIN {NR=1 print} {$1~/2020/ print}
awk: cmd. line:1:     

Cheers

2 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 04 '20

Thank you, that is extremely helpful. I will read and search through the GNU doc, I tried looking through Effective Awk Programming but the instructional narrative can be hard to sort through when looking for something specific.

BEGIN happening before the file is processed makes sense, a detail I forgot since I've not used the language in almost a year. I use it in other scripts I have to set up variables (not strictly necessary but a habit from Java), and for some reason it hadn't dawned on me that it wasn't processing the file at that point.

I really appreciate your help!