Reputation: 3
Being quite new to Linux and awk, this problem has me stuck. Bet it's easy to get it working. So I have this command that works fine. Printing only lines newer than a minute timestamp as text.
awk -F\" '$0> "[01/Nov/2018:15:58" {print $0}' localhost_access_log.2018-11-01.txt
The log file is formatted as follows
[01/Nov/2018:15:53:16 +0200] x.x.x.10 "-" "-" "libwww-perl/6.13" "GET /manager/status?XML=true HTTP/1.1" 401 2473 1
[01/Nov/2018:15:53:16 +0200] x.x.x.10 "-" "-" "libwww-perl/6.13" "GET /manager/status?XML=true HTTP/1.1" 200 3070 4
[01/Nov/2018:15:58:16 +0200] x.x.x.10 "-" "-" "libwww-perl/6.13" "GET /manager/status?XML=true HTTP/1.1" 401 2473 2
[01/Nov/2018:15:58:16 +0200] x.x.x.10 "-" "-" "libwww-perl/6.13" "GET /manager/status?XML=true HTTP/1.1" 200 3070 4
But running the same command from a bash script won't work correctly. This below prints all lines from the log file.
#!/usr/bin/bash
STAMP=$(date --date '-15 min' "+%d/%b/%Y:%H:")
MIN1=$(date --date '-15 min' "+%M")
MIN1=${MIN1:0:2}
STAMP=$STAMP$MIN1
LOGSTAMP=$(date +%Y-%m-%d)
awk -F\" '$0> "$STAMP" {print $0}' localhost_access_log.$LOGSTAMP.txt
And changing "$STAMP" -> $STAMP won't print anything.
awk -F\" '$0> $STAMP {print $0}' localhost_access_log.$LOGSTAMP.txt
Can you help?
Upvotes: 0
Views: 1645
Reputation: 753665
To get the shell variable into the Awk program string, you need to terminate the single quote string before and restart it after referencing the (quoted) variable. That gets intricate:
awk -F'"' '$0> "'"$STAMP"'" {print $0}' localhost_access_log.$LOGSTAMP.txt
What awk sees as the program is then:
$0 > "01/Nov/2018:13:15" { print $0 }
Upvotes: 1
Reputation: 50034
Bash variables don't work in awk
. You have to pass them into your awk script to an awk variable by using the -v
flag:
#!/usr/bin/bash
STAMP=$(date --date '-15 min' "+%d/%b/%Y:%H:")
MIN1=$(date --date '-15 min' "+%M")
MIN1=${MIN1:0:2}
STAMP=$STAMP$MIN1
LOGSTAMP=$(date +%Y-%m-%d)
awk -F\" -v stamp="$STAMP" '$0>stamp {print $0}' localhost_access_log.$LOGSTAMP.txt
Upvotes: 3