Reputation: 1
I have begun using AWK to reduce the number of lines in CSV files I use. My files usually have between 60,000 and 300,000 lines and I reduce them to 5,000 lines using this (with the number 38 changed as necessary):
awk ’NR % 38 == 0’ input.csv > output.csv
This works, by using the 2nd argument as the input file and the 3rd argument as the output file.
I'm trying to use the first argument "$1" to replace the number 38. I can not make AWK use the argument in this way however. Below is what I'm trying to accomplish...
sh reduce.sh 1000 input.csv output.csv
#!/bin/bash
#script name is reduce.sh
awk ’NR % $1 == 0’ $2 > $3
Thanks in advance for any help.
Upvotes: 0
Views: 31
Reputation: 246807
First thing, your quotes are wrong: they look like fancy "smart" quotes. Make sure they are plain single quotes
awk ’NR % $1 == 0’ $2 > $3
# ..^............^
Next, bash variables are not expanded inside single quotes. The best way to pass a shell variable to awk is with the -v
option
awk -v step="$1" 'NR % step == 0' "$2" > "$3"
Last, always quote your variables
Upvotes: 4