简体   繁体   中英

processing JSON file in bash using jq

I have a bash script

counter=0
for file in /home/ec2-user/Workspace/events_parts/*
do
        counter=$[counter + 1]
        event=$(cat $file | jq '.Event')
        echo $event
        if [ "$event" = "Time Exceeded" ]  || [ "$event" = "Load Time" ]; then
                echo "Coming Here"
                jq ".url = \"$(jq '.Message' $file | sed 's/.*proxy=\([^&]*\).*/\1/')\"" $file 
        else
                jq ".url = null"
        fi
done
~   

In the bash script above I am trying to extract Event field from a JSON file and check for two possible values. if [ "$event" = "Time Exceeded" ] || [ "$event" = "Load Time" ]; is not workign as I would expect. I have verified that the values that I am comparing against are indeed there.

Check once if there are leading or trailing unwanted characters (whitespace, newline ...) in output of cat $file | jq '.Event' cat $file | jq '.Event' :

cat $file | jq '.Event' | hexdump -C

Update:

Replace

[ "$event" = "Time Exceeded" ]

by

[ "$event" = "Time Exceeded." ]

or replace by

[ "${event%.}" = "Time Exceeded" ]

or replace by

[[ "$event" =~ "Time Exceeded" ]]

to match a substring. ${event%.} crops a trailing . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM