1

I'm filtering the fourth column in a file using below command. it's working fine in the console

[User@ipaddress Now]$ creationTime="8:15 PM"
[User@ipaddress Now]$ awk -F, -v var="$creationTime" '{if($4==var) print}' input.txt
serial1,tech,EU,8:15 PM,gan

Added the same command in shell script but it's not working.

$ cat test.sh
creationTime=$(date -d '330 minutes'  +"%l:%M %p")

echo $creationTime 
awk -F, -v var="$creationTime" '{if($4==var) print}' input.txt

Output while execting the script

[User@ipaddress Now]$ sh test.sh
8:15 PM

input.txt

serial1,tech,APAC,8:09 PM,anz
serial1,tech,EU,8:15 PM,gan
1
  • Since the time is calculate dynamically in the script, the timestamp might simply not being part of input.txt Commented Sep 7, 2016 at 15:19

1 Answer 1

2

The problem might be that $creation_time contains leading spaces. When you inspect it with echo, they might not be visible, but they will definitely not match $4 in awk. Example:

$ creationTime=$(date -d '330 minutes'  +"%l:%M %p")
$ echo ">${creationTime}<"
> 4:50 PM<

Try instead:

creationTime=$(date -d '330 minutes'  +"%l:%M %p" | sed 's/^ *//')
Sign up to request clarification or add additional context in comments.

Comments

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.