Flow
Flow

Reputation: 119

Is there any way to pass a number stored in a variable to awk for this command?

I am trying to build a script that takes the days to go back in the log review/analysis as argument:

script.sh 5

this would show the detailed traffic for the past 5 days on an account. It is based on this command:

zcat $path/* |awk -vDate=`date -d'now-5 days' +[%d/%b/%Y:%H:%M:%S` ' { if ($4 > Date) print $1, $12, $15, $16, $17, $23, $24}' | sort | uniq -c | sort -fr | head

How can I store that days number as a variable and pass it over to awk?

days=$1
zcat $path/* |awk -vDate=`date -d'now-$days days' +[%d/%b/%Y:%H:%M:%S` ' { if ($4 > Date) print $1, $12, $15, $16, $17, $23, $24}' | sort | uniq -c | sort -fr | head

Upvotes: 0

Views: 46

Answers (1)

Ed Morton
Ed Morton

Reputation: 203324

You're not passing a variable to awk, you're passing it to date

awk -v date="$(date -d "now-$days days"  +"[%d/%b/%Y:%H:%M:%S")" 'script'

Upvotes: 1

Related Questions