Couldn't find anything on this, but I'm trying to only log a specific part of stderr to a file. So for example

Running tar -xzf file 2>> log.txt, and getting an error

tar: Child returned status 1

I only want to log the Child returned status 1 part, and not include the command name.

  • 1
    You would have to filter stderr through some process. However, removing the process name removes vital debugging info, so I would not do that. – thrig Oct 2 '17 at 14:15
  • Do you want to remove the tar: prefix off every error message by tar, or just that one and discard every other error message by both tar and gzip? – Stéphane Chazelas Oct 2 '17 at 22:10
tar -xzf file 3>&1 1>&2 2>&3 | grep -oP '^app: \K.*$' | tee logfile
| improve this answer | |

With zsh, you can do:

tar -xzf file 2> >(sed 's/^tar: //' >> file.log)

Which allows to retain tar's exit status.

You can do something similar with ksh, bash or yash though you'd have to use work arounds there to make sure sed has finished by the time you run the next command if that command makes use of the file.log.

Other option with bash is to use:

{ tar -xzf file 2>&1 >&3 3>&- | sed 's/^tar: //' >> file.log 3>&-; } 3>&1

Note that with several shells, you can also call tar with an empty argv[0] with:

(exec -a '' tar -xzf file)

In which case the error messages will look like:

: Child returned status 1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.