0

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.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
FatArmpit
  • 3
  • 2

2 Answers2

1

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    This redirects stdout, not stderr. To pipe stderr, you'll need a `2>&1` before the `|`, but that will pipe both stdout and stderr, so you might want a `>file` before *that* to send stderr somewhere else first – Chris Dodd Oct 21 '22 at 07:22
  • 1
    Although it didn't solve the issue I was facing, it made me think more about other syntax that I could use if the situation was different. Thanks for taking the time to reply and try to help me! It led me to read more about different cases. – FatArmpit Oct 23 '22 at 11:51
1

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.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • Sorry for the delay in reply. I although I had to make slight modifications to make it work for the situation I'm tackling, your answer got me 90% of the way. The link you've provided to process substitution and other articles related to the topic quickly got me up to speed with how to resolve it. Thanks! – FatArmpit Oct 23 '22 at 11:49