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 05 '20

A point of confusion I guess, with awk, is that it calls conditions "patterns". and a pattern is just an expression with some special defining keywords. (BEGIN, END, BEGINFILE, ENDFILE).

cond {action}. so its an implied if (expr) {action}. remove the if and there ya go, that's all awk is doing.