我想將檔案夾中所有檔案的多個不同字串更改為一個新字串。
當文本檔案中的字串(在同一目錄中)是這樣的:
- 檔案 1.json:
"url/1.png" - 檔案 2.json:
"url/2.png" - 檔案 3.json:
"url/3.png" - 等等
我需要將它們全部指向一個 PNG,即 ,"url/static.png"因此所有三個檔案都具有指向同一個 PNG 的相同 URL。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
您可以為此使用命令 find 和 sed。確保您位于要替換檔案的檔案夾中。
find . -name '*.*' -print|xargs sed -i "s/\"url\/1.png\"/\"url\/static.png\"/g"
uj5u.com熱心網友回復:
建議bash腳本:
#!/bin/bash
# for each file with extension .json in current directory
for currFile in *.json; do
# extract files ordinal from from current filename
filesOrdinal=$(echo "@currFile"| grep -o "[[:digit:]]\ ")
# use files ordinal to identify string and replace it in current file
sed -i -r 's|url/'"$filesOrdinal".png'|url/static.png|' $currFile
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470758.html
