在 bash 中使用 jq 处理 JSON 文件

processing JSON file using jq in bash

我有以下 bash 脚本

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=\([^&]*\).*//')\"" $file  > events_parts/out_$file
        else
                jq ".url = null" $file > events_parts/out_$file
        fi
done

它处理一组 JSON files.I 想要将其重定向到一个文件名有 out_$input_file_name。但是变量文件打印出整个路径而不仅仅是文件名(例如 /home/ec2-user/Workspace/events_parts/input.json)我如何从中得到 "input file name"?

试试这个:

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

输出:

input.json