2

I'm trying to write a script to pull logs between two timestamps using awk.

So this works :

awk '$0 >= "07-Nov-2021 13:40:02.124" && $0 <= "07-Nov-2021 13:43:08.124"' websniffer.log

But when I use it within a bash script, I'm struggling to get it work. Here's my minimal sample script & error :

#!/bin/bash

fromtime=$1
totime=$2
nameoffile="websniffer.log"
awk -v fromtimestamp=$fromtime -v totimestamp=$totime -v filenm=$nameoffile '$0 >= "$fromtimestamp" && $0 <= "$totimestamp"}' $filenm

Now when I execute this script by :

#terminal@root$ ./samplescript.sh "07-Nov-2021 13:40:02.124" "07-Nov-2021 13:43:08.124"

I get this :

 ./samplescript.sh "07-Nov-2021 13:40:02.124" "07-Nov-2021 13:43:08.124"./samplescript.sh "07-Nov-2021 13:40:02.124" "07-Nov-2021 13:43:08.124" ./samplescript.sh: line 6:  2190 Segmentation fault      awk -v fromtimestamp=$fromtime -v totimestamp=$totime -v filenm=$nameoffile '$0 >= "$fromtimestamp" && $0 <= "$totimestamp"}' $filenm

Can someone please help in getting this working ?

3
  • Different from the shell, in an awk program you do not access the value of a variable with $varname. Just use varname. Commented Nov 15, 2021 at 10:07
  • The date format you have there is not sortable by awk. Use the time functions to reformat that time stamp into a ISO 8601 type time stamp. You can also use GNU date on Linux to do the same. Commented Nov 15, 2021 at 13:24
  • 2
    Are you SURE that's all you're doing? The script has obvious errors but I don't see anything in it that would cause bash to report a segmentation violation, the error you should be getting from that script is a syntax error from awk as it tries to interpret 13:40:02.124 as a script. if your shell is segfault-ing on that script then your shell is broken and you should get a new one. Commented Nov 15, 2021 at 13:35

1 Answer 1

1

There's a couple of things:

#!/bin/bash

fromtime=$1
totime=$2
nameoffile="websniffer.log"
awk -v fromtimestamp="$fromtime" -v totimestamp="$totime" '$0 >= fromtimestamp && $0 <= totimestamp' "$nameoffile"

You need to quote the shell variables you pass to awk (they have whitespace in them) and there's no need to create filenm and pass it to awk since you're not using it within the awk script.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I tried your version & there seems to be a space somewhere...not sure where it is complaining about a whitespace(If that's the issue here ) ./samplescript.sh "07-Nov-2021 13:40:02.124" "07-Nov-2021 13:43:08.124" awk: cmd. line:1: Unexpected token
@JefSmackner - sorry, did some more cleanup. That's what happens when I don't test stuff. Try the modified version, please.
@JefSmackner - does the amended script do what you expect?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.