简体   繁体   中英

processing JSON file using jq in bash

I have the following 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" = "Page Exceeded" ]  || [ "$event" = "Reporting Time" ]; then
                echo "Coming Here"
                jq ".url = \"$(jq '.Message' $file | sed 's/.*proxy=\([^&]*\).*/\1/')\"" $file  > events_parts/out_$file
        else
                jq ".url = null" $file > events_parts/out_$file
        fi
done

It does processing of a set of JSON files.I want to redirect it to a filename which has out_$input_file_name. But the variable file prints out the whole path not just the file name (for eg /home/ec2-user/Workspace/events_parts/input.json ) How do I get just the "input file name" from this?

Try this:

file="/home/ec2-user/Workspace/events_parts/input.json"
filename="$(basename "$file")"
echo "$filename"

Output:

input.json

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