使用 tee 命令写入文件时如何忽略某些字符

How to ignore certain characters while writing to file using tee command

我有一个 zsh 脚本将输出通过管道传输到一个文件,即

<command here> | tee -a testFile

我的问题是命令的一些输出是字符“[]”。我不想将这些写入我的 testFile,但应将其他任何内容写入该文件(例如 ["Hello"])。我怎样才能过滤写入我的 testFile 的内容? TIA

我希望输出如下:

["Hello"]
["World"]
["One"]
["Two"]

..等

但目前我得到的是这样的:

[]
[]
[]
["Hello"]
["World"]
[]
[]
["One"]
[]
["Two"]

到目前为止我已经尝试过:

<command here> | sed -E 's/\[|\]//g' | tee -a testFile

但这也会导致不正确的输出(注意 [] 被替换为空,然后作为新行添加到文件中):

["Hello"]
["World"]


["One"]

["Two"]

假设要删除的字符“[]”单独出现在没有 任何其他 leading/trailing 个字符(如提供的示例中所述), 你能试试吗:

<command here> | sed -nE '/^\[]$/!p' | tee -a testFile

输出:

["Hello"]
["World"]
["One"]
["Two"]