unix - awk command is working in the console but not working inside shell script -


i'm filtering fourth column in file using below command. it's working fine in 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 same command in shell script 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 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 

the problem might $creation_time contains leading spaces. when inspect echo, might not visible, 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/^ *//') 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -