我有以下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
)如何从中获取“输入文件名”?
答案 0 :(得分:1)
试试这个:
file="/home/ec2-user/Workspace/events_parts/input.json"
filename="$(basename "$file")"
echo "$filename"
输出:
input.json