Reputation: 7021
I am trying to parse some nginx logs by date. I have written an awk oneliner to find all matching logs for a day, like:
awk '/05\/May\/2012/ { print $0;}' /var/log/nginx/access.log
What I want to do is to loop over some days and months using bash. Here is what I have got
#!/bin/bash
for h in 2012
do
for i in Apr May
do
for j in 01 02 03 04 05
do
echo "/${j}\/${i}\/${h}/"
search_string="'/${j}\/${i}\/${h}/ { print \$0;}'"
echo $search_string;
awk $search_string /var/log/nginx/access.log
echo "${h} ${i} ${j} done"
done
done
done
However, this fails on awk line with:
awk: 1: unexpected character '''
It seems like I need to escape some variables, but I haven't found a solution so far.
Upvotes: 0
Views: 222
Reputation: 359895
Here is your entire script in one AWK script:
awk 'BEGIN {
years = "2012"
months = "Apr|May"
days = "01|02|03|04|05"
}
$0 ~ days "/" months "/" years {
print
}' /var/log/nginx/access.log
If you need the start and done markers, those can be added.
Upvotes: 3
Reputation: 274532
It's better to use awk's variable assignment feature to pass the shell variable into awk. Then use ~
to match the pattern. For example:
search_string="$j/$i/$h"
awk -v pattern="${search_string}" '$0 ~ pattern {print $0}' /var/log/nginx/access.log
This is the recommended method specified in the awk manual.
Upvotes: 6
Reputation: 161614
Single quotes are not needed. Double quotes are needed.
search_string="/${j}\/${i}\/${h}/ { print \$0;}"
awk "$search_string" /var/log/nginx/access.log
Upvotes: 2