【问题标题】:processing JSON file using jq in bash在 bash 中使用 jq 处理 JSON 文件
【发布时间】:2015-03-18 00:36:16
【问题描述】:

我有以下 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)我如何从中获取“输入文件名”?

【问题讨论】:

标签: json bash jq


【解决方案1】:

试试这个:

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

输出:

输入.json

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多