0

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

它处理一组 JSON 文件。我想将它重定向到具有 out_$input_file_name 的文件名。但是变量文件打印出整个路径而不仅仅是文件名(例如/home/ec2-user/Workspace/events_parts/input.json)我如何从中获取“输入文件名”?

4

1 回答 1

1

尝试这个:

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

输出:

输入.json
于 2015-03-18T00:52:21.967 回答