Home > other >  Customised redirection of stderr?
Customised redirection of stderr?

Time:10-21

I know that I can redirect the standard error output stream by: $ ./my-C-binary 2>error.log

What if I want to ignore the first two characters of anything from stderr before writing to the file? I've searched but I can't seem to find a way to do so. I'm not allowed to re-build my-C-binary (i.e. not allowed to change the source code). I am also not allowed to write an additional program to help with the task, it must be solved with a single command line to modify the 2>error.log portion. I don't know how to do that even after a few days of searching and trying.

Let's say the output of stderr is:

The quick brown fox jumps over the lazy dog.

The text in error.log must be:

e quick brown fox jumps over the lazy dog.

Thanks in advance for any suggestions and help.

CodePudding user response:

You can pipe the output through the tail command to ask it to skip the first two bytes:

$ ./my-C-binary |& tail -c  2 >error.log

Unfortunately this will pipe both standard error and standard output. To only get standard error, you need to do it in two steps:

  1. Run the program like you do now, redirecting standard error to its file
  2. Modify the file to "remove" the two first characters

CodePudding user response:

Combining this answer using process substitution to redirect stderr in bash with @Someprogrammerdude's use of tail:

./my-C-binary 2> >(tail -c 3 > error.log)

As an aside, note the " 3" parameter to tail: The number indicates the 1-based index of the character where output starts; if you want to skip the first two characters, you start with the third.

CodePudding user response:

MISREAD -------------------------------------------------------------

  • Related