I have a large CSV file. Basically what I want to do is display only the first 5 columns (which I can) but I also only want to display the first 10 rows, which is what I can't figure out. Right now this is the .awk file script I have:
"BEGIN {FS = ","}
{print $1", " $2", " $3", " $4", " $5}"
In Tera Term this is the command I use, which results in the first 5 columns of every row to be displayed: $awk -f example.awk example.csv
I have tried a couple of ways of using NR<=11 but that keeps coming up with error messages when trying to run on Tera Term.
Please help!
Try:
awk -F, '{print $1,$2,$3,$4,$5} NR==10{exit}' OFS=', ' file.csv
[Note that the awk code is in single-quotes, not double-quotes. That is necessary to prevent the shell from misinterpreting $1
, etc., as shell variables and expanding them.]
-F,
tells awk to use ,
as the field separator on input. This is equivalent to but much briefer than BEGIN {FS = ","}
.
print $1,$2,$3,$4,$5
tells awk to print the first five columns. (See below for why the ", "
strings aren't needed in this version.)
NR==10{exit}
tells awk to exit on the tenth line.
OFS=', '
tells awk to use comma-space as the field separator on output. This setting makes much of the quoting in the original code unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With