David
David

Reputation: 3105

awk working with intervals

I have this file

goodtime 20:30 21:40
badtime 19:52 24:00

and when I enter for example 21:00 and 21:15 I should get goodtime

So here's my script

#!/bin/sh

last > duom.txt

grep -F 'stud.if.ktu.lt' duom.txt > ktu.txt

echo "Nurodykite laiko intervala "
read h
read min
read h2
read min2

awk '{if ($2 ~ /$h.$m/ && $3 ~ /$h2.$min2/) print $1}' data.txt

But I don't get any results.

Upvotes: 0

Views: 36

Answers (1)

glenn jackman
glenn jackman

Reputation: 246817

The problem with this:

awk '{if ($2 ~ /$h.$m/ && $3 ~ /$h2.$min2/) print $1}' data.txt

Is that you're trying to use shell variables in a single quoted string. You need to pass the shell variables into awk with its -v option:

awk -v patt1="$h.$min" -v patt2="$h2.$min2" '
    $2 ~ patt1 && $3 ~ patt2 {print $1}
' data.txt

But, given your sample input, this will not match anything.

Until your requirements are clarified, I can't help with the logic.

Upvotes: 1

Related Questions