繁体   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