I assume that you still want to perform text-processing operations with awk
on this CSV file. If so, I would recommend adding a condition on the "line number" to it, as in:
awk -F',' 'NR==1{print} NR>1{ your code here }' foo.csv
Here, NR
is the awk
builtin variable for the "record number", which usually defaults to the line number (notice that when processing multiple files, this is the "global number of processed lines", the per-file-line number is FNR
).
You can also easily omit printing the header by leaving out the NR==1{...}
part.
If in the end you will be using print
in your manipulations anyway, you can "golf" this to
awk -F',' 'NR>1{ your code here }1' foo.csv
the 1
standing for "print the resulting line ($0
)".
Also:
- you don't need to
cat
a file to pipe it to awk
, just supply it as command-line argument
- variables that are uninitialized default to "0", so you don't really need the
start=0
statement in your BEGIN
section
NR == 1 {print; next;}
– muru Feb 10 '20 at 15:41BEGIN{if(getline>0)print}
at the beginning of your script. If you want to process multiple files, then theNR>1{...}
you see in thousands of examples (including the aswer to this Q) is WRONG, you needFNR>1{...}
instead: you either want to skip the header of each file, or if you don't, you also don't want to do a pointless test for each line. – Feb 10 '20 at 16:36