start_time="2021-02-10 08:17:38"
end_time="2021-02-10 09:17:38"
ssh [email protected] "awk -v start_tm=$start_time -v end_tm=$end_time '\$0 >= start_tm && \$0 <= end_tm' /var/log/app.log"

I am facing below error.

awk: cmd. line:1: 08:17:38
awk: cmd. line:1:   ^ syntax error

The same awk command is working if I execute after SSH to the server. even I tried to keep backslash \ before & command. I am new to shell scripting, please suggest what other things need to take care.


1 Answer 1


If you are going to call the awk script here, always it's good practice to have a script and call the script so you no need worry about inner variables escaping:

ssh user@localhost "\
    awk -v start='$start_time' -v end='$end_time' -v q=\' \
        -f /path/to/awk-script.awk /path/to/log-file"

also note that we used escaped $start_time and $end_time above with quotes.

awk-script.awk contains:

        st = "date -d" q start q " +%s"
        st | getline start
        ed = "date -d" q end q " +%s"
        ed | getline end
        dt = $1 " " $2
        epoch = "date -d" q dt q " +%s"
        epoch | getline dt

(dt >= start && dt <= end) { print }
  • can't we use '\$0 >= start_tm && \$0 <= end_tm' instead of script? since when I tested this is working fine. so that my command will become single line statement.
    – bhr
    Feb 18, 2021 at 17:53
  • @bhr you are doing date comparison but awk has no "date comparison" feature. the way you are doing like that is just integer comparison like doing awk 'BEGIN{ print (3==3x?"equal":"not-equal")}' vs awk 'BEGIN{ print (3=="3x"?"equal":"not-equal")}' or awk 'BEGIN{ print (3==3x+0?"equal":"not-equal")}'. Feb 18, 2021 at 18:00
  • ok, I understood. But for my understanding purpose what's the mistake I am doing? To make it work what's need to be change in my command ?
    – bhr
    Feb 19, 2021 at 6:44
  • @bhr ssh [email protected] "awk -v start_tm='$start_time' -v end_tm='$end_time' '\$0 >= start_tm && \$0 <= end_tm' /var/log/app.log" Feb 19, 2021 at 6:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .