Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
Bash & awk : Segmentation fault while trying to use bash script arguement in awk

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 ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!