7

I am trying to run the following command

./someprogram | tee /dev/tty | sed 's/^.\{2\}//' > output_file

But the file is always blank when I go to check it. If I remove > output_file from the end of the command, I am able to see the output from sed without any issues.

Is there any way that I can redirect the output from sed in this command to a file?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
lacrosse1991
  • 2,972
  • 7
  • 38
  • 47
  • Is it possible that `sed` prints to `stderr` instead of `stdout` for some reason? – The Paramagnetic Croissant Apr 12 '14 at 05:17
  • What's the output of `someprogram`? – Reinstate Monica Please Apr 12 '14 at 05:49
  • It works for me just as is: `sed` succeeds in removing the first two characters and the result ends up in `output_file`. – John1024 Apr 12 '14 at 05:52
  • replace the `> outfile` with `| cat - -vet` . This will let you confirm that data is actually coming out of std-out from `sed`, and it will show control chars like `^M,^I` and mark the end of each line with `$`. If you can't reach a conclusion as to why it's not working from that output, there are fancier tools we can use on the output, but using them gets more difficult to explain. My guess is that `someprogram` is output `^M$` at the end of the line. – shellter Apr 12 '14 at 12:03
  • @shellter hello, naab's suggestion of supplying the -u option to sed ended up doing the trick. With cat - -vet, a '$' is placed at the end of each line, although that is the only additional output that I get – lacrosse1991 Apr 12 '14 at 16:58
  • also the program is a game server called dedcon http://dedcon.homelinux.net/bin/dedcon-i686-pc-linux-gnu-1.6.0_svn1038.tar.bz2. I am trying to run this command on it's console/output – lacrosse1991 Apr 12 '14 at 16:59

2 Answers2

9

Remove output-buffering from sed command using the -u flag and make sure what you want to log isn't on stderr

-u, --unbuffered

  load minimal amounts of data from the input files and flush the output buffers more often

Final command :

./someprogram | tee /dev/tty | sed -u 's/^.\{2\}//' > output_file

This happens with streams (usually a program sending output to stdout during its whole lifetime).

sed / grep and other commands do some buffering in those cases and you have to explicitly disable it to be able to have an output while the program is still running.

Community
  • 1
  • 1
naab
  • 1,122
  • 1
  • 8
  • 25
1

You got a Stderr & stdout problem. Checkout In the shell, what does " 2>&1 " mean? on this topic. Should fix you right up.

Community
  • 1
  • 1
Jason McD
  • 567
  • 1
  • 4
  • 12