簡體   English   中英

優化 shell 腳本中的多個 sed 命令

[英]optimize multiple sed commands in shell script

我有一個文件夾,其中包含許多帶有 json 內容的文本文件。 使用 jq 庫,我可以提取“商品”數組並將其寫入文件。 “commodities-output.txt”是一個臨時文件,除了數組中的字符串值之外,還包含括號“[”、“]”和“null”值。 我想刪除方括號,“null”值並獲取文本文件中的唯一字符串值。 有沒有辦法優化 sed 命令,這樣我就不必創建諸如“commodities-output.txt”之類的臨時文本文件,並且只有一個 output 文件,其中包含我需要的所有字符串值,這些值是唯一且已排序的(可選進行排序)。

$F=foldername
for entry in $F*.json
do
  echo "processing $entry"
  jq '.[].commodities' $entry >> commodities-output.txt
done
sed '/[][]/d' commodities-output.txt | sed '/null/d' commodities-output.txt | sort commodities-output.txt | uniq >> commodities.txt

echo "processing complete!"

您可以在jq中輕松完成所有這些操作。

files=( "$F"*.json )
echo "$0: processing ${files[0]}" >&2
xargs jq '.[] | select(.commodities != [] and .commodities != null) | .commodities' "${files[0]}"

我重構為使用 Bash 數組來獲取第一個匹配文件。

如果由於某種原因您無法重構代碼以完全在jq中運行,那么您肯定更喜歡管道而不是臨時文件。

for entry in $F*.json
do
  echo "$0: processing $entry" >&2
  jq '.[].commodities' "$entry"
  break
done |
sed -e '/[][]/d' -e '/null/d' |
sort -u > commodities.txt

還要注意我們如何注意將進度診斷打印到標准錯誤 ( >&2 ) 並在診斷消息中包含腳本的名稱。 這樣,當您有運行腳本的腳本運行腳本時,您可以看到哪一個需要您的注意。

...
# write to target file, no temp needed
jq '.[].commodities' $entry >> commodities.txt
...
# You can read it with first sed command and pipe the output to next sed command (it reads stdin) and to the next commands
# Also, sort has -u flag that do the same as uniq, so you don't need a separate command
# At the end rewrite your target file with the result from sort
sed '/[][]/d' commodities.txt | sed '/null/d' | sort -u > commodities.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM