Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why cant I redirect the output from sed to a file

Tags:

bash

debian

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?

like image 721
lacrosse1991 Avatar asked Apr 12 '14 05:04

lacrosse1991


People also ask

How do I redirect the output of a sed file?

$ sed -f sedscr testfile > newfile The redirection symbol “>” directs the output from sed to the file newfile. Don't redirect the output from the command back to the input file or you will overwrite the input file. This will happen before sed even gets a chance to process the file, effectively destroying your data.

Is it possible to redirect the sed operated data to some file other than the original?

sed is not meant for data redirection as tee and > meant to be. However you can use conjunction of commands to do that.

How can you redirect the output of a command to a file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


1 Answers

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.

like image 91
naab Avatar answered Sep 30 '22 14:09

naab