美文网首页
shell批量处理json脚本

shell批量处理json脚本

作者: 卡卡罗忒 | 来源:发表于2021-11-04 14:42 被阅读0次

第一次写一个完成度较高的脚本,shell好用但是难写

#!/bin/bash
# 处理无他相机的数据包,解决业务渲染耦合问题
if [ `command -v jq` ];then
    echo 'jq 已经安装'
else
    echo 'jq 未安装,开始安装json解析工具'
#安装jq
    brew install jq
fi
prefix="https://res-release.wuta-cam.com/components/"
filterNameArr=$(cat  component_tree.json|jq ".cosmetic[].name")
componentNum=0
#下载文件
for filterName in ${filterNameArr}; do
    floder=${filterName//\"}
    echo 创建文件夹
    mkdir "${floder}"
    cd "${floder}"
    echo 下载
    getSubTypeRes=$(cat  ../component_tree.json|jq ".cosmetic[${componentNum}].components[].package")
    for subTypeRes in ${getSubTypeRes}; do
        name=${subTypeRes//\"}
        fileURL=${prefix}${name}
        curl -o ${name} ${fileURL}
    done
    componentNum=`expr ${componentNum} + 1`
    cd ..
done
open .
#解压处理
allFloder=$(ls $pwd);
for floder in ${allFloder}; do
    if [ -d ${floder} ]; then
        cd ${floder}
        for file in $(ls $pwd); do
            if [ "${file##*.}"x == "zip"x ];then
            echo "正在解压$file"
            unzipfloader=${file%%.*}
            unzip ${file} -d ${unzipfloader} 
            echo "解压完成"
            echo "删除原始压缩包${file}"
            rm -r ${file}
            cd ${unzipfloader}
            filename="index.json"
        

                echo "拼接cosmetic和type"
                #单独处理眼妆
                if [[ $floder == "a_yanzhuang" ]]; then
                    sed -i "" "2 i\ 
                    "\"cosmetic\":{"
                    " index.json

                    sed -i "" "$ a\ 
                    "}"
                    " index.json
                else
                    #处理非集合
                    newStr="\"cosmetic\" :{ \"${floder}\":{"
                    line=$(grep -n "KEY_MODULE_SET" $filename | cut -d ":" -f 1 | head -1)
                    #拼接cosmetic和type
                    sed -i "" "${line} i\ 
                            ${newStr}
                            " index.json
        
                    numInArr=0
                    for names in $filterNameArr; do
                        if [[ ${names//\"} = $floder ]]; then #查到对应的type,获取params,写到index.jso
                            paras=$(cat  ../../component_tree.json|jq ".cosmetic[${numInArr}].feature.items[0].params" | jq "tostring")
                                if [[ ${paras} =~ 'null' ]]; then
                                    echo 没有paras字段,跳过
                                else
                                    paras=${paras#*\"}
                                    paras=${paras%*\"}
                                    totalLine=$(grep -c "" index.json)
                                    totalLine=`expr ${totalLine} - 3`
                                    T="\"params\":${paras},"
                                    sed -i "" "$totalLine a\ 
                                                $T
                                                " index.json
                                fi
                            break

                        fi
                    numInArr=`expr ${numInArr} + 1`
                    done

                sed -i "" "$ a\ 
                    "}}"
                    " index.json
                fi
            cd ..
            fi
    done
    cd ..
    fi
done

相关文章

网友评论

      本文标题:shell批量处理json脚本

      本文链接:https://www.haomeiwen.com/subject/oukkzltx.html