Can't make AWK process an argument correctly

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.

Answers


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


Need Your Help

Setting up Python in Netbeans

python netbeans python-3.x

I remember successfully doing this countless times before with a bit of trial and error, but after a new reinstall of Snow Leopard, I would like to just ask the Stackoverflow community this for onc...