Home > Enterprise >  How to ignore certain characters while writing to file using tee command
How to ignore certain characters while writing to file using tee command

Time:04-19

I have a zsh script that's piping the output to a file, i.e

<command here> | tee -a testFile

my issue is that some of the outputs from the command are the characters "[]". I do not want these written to my testFile, but anything else should be written to the file (e.g. ["Hello"]). How can I go about filtering what gets written to my testFile? TIA

I want the output to be like:

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

.. etc

but currently I'm getting something like this:

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

So far I've tried:

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

but this also leads to an incorrect output of (note [] being replaced by nothing and then that being added to the file as a new line):

["Hello"]
["World"]


["One"]

["Two"]

CodePudding user response:

Assuming the characters "[]" to be removed appear alone in the line without any other leading/trailing characters (as described in the provided example), would you please try:

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

Output:

["Hello"]
["World"]
["One"]
["Two"]
  • Related