Shell-Script

我應該如何使用 sed 命令用 md5 替換文件中的數字?

  • May 18, 2020

我有一個大日誌文件,其中包含如下範例:

{"data_1":210,"target_number":1096748811,"extra_data":66}
{"data_1":0,"target_number":7130881445,"extra_data":56}
{"data_1":1712,"target_number":1098334917,"extra_data":48}
{"data_1":0,"target_number":3062674667,"extra_data":54}
{"data_1":53,"target_number":5110609228,"extra_data":246}

我想用整個文件中的值評估替換target_number的值。md5

我正在嘗試sed使用以下基本語法的命令:

sed -i 's/target_number/target_number_md5/' input.log

預期輸出為:

{"data_1":210,"target_number":620e25e6f054992308c564cb883e4940,"extra_data":66}

我自己找到了答案。

cat $inputFile | grep -Po '(?<="target_number":)[^,"]+' | while read i ; do
   sed -i "s/$i/$(echo -n $i | md5sum | cut -f 1 -d ' ')/g" $outputFile
done

引用自:https://unix.stackexchange.com/questions/587379