1

I have a script that takes several CSV files from a folder and puts them together in a gzip file with one field extracted from the file name :

awk -F";" 'BEGIN { OFS = FS }
FNR == 1 { match(FILENAME, "([0-9]{8})", a); JOBID = a[1]; print $0, JOBID }
FNR > 1 { print $0, JOBID }' Event_*txt | gzip > agzipfile.gz

My goal is to use NF to check if the line contains 5 fields and if not print this line to stderr or to another file.
But I didn't understand or find a proper way of doing this.
Is it possible and how ?
PS: I'm using GNU awk

1 Answer 1

3

You can redirect inside AWK:

NF != 5 { print > "/dev/stderr"; next }

/dev/stderr is one of the special filenames which GNU AWK processes internally. (Other AWK implementations will write to /dev/stderr, if possible; in many systems that’s connected to standard error anyway, so the result is the same.)

See this section of the manual on redirections, and this one on special file descriptors.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .