>(tee -a ./lo" /> >(tee -a ./lo" /> >(tee -a ./lo"/>

Insert a text at the beginning of a file + stdout, stderr

633 Views Asked by At

I have function "backup", and output for this function from the console is also redirected to a file, and it's done as follows:

backup > >(tee -a ./log.txt) 2>&1

it works, but I want to add new output data to the beginning of a file, and looks like for my case wisely use ed (not sed), and I doing it in the following way:

ed -s log.txt < <(printf '%s\n' 1i "$(backup)" . wq)

And in this case, I don't know how to implement the output to the console and the file at once (as it happens in my first case). Can somebody give me a hint of implementation?

2

There are 2 best solutions below

0
Mark On

If I understand your code correctly - you are trying to pre-pend the output of the backup command to the start of the log.txt file. Moreover, you are trying to see the backup output on the console.

Try this:

{
  backup 2>&1 | tee /dev/tty
  cat log.txt
} > log.txt.new
mv log.txt.new log.txt 

Edit:

Here's a version of the code that follows Charles Duffy's suggestion:

cat <( backup 2>&1 | tee /dev/tty ) log.txt > log.txt.new && mv log.txt.new log.txt
0
Omid N On

Insert a text to the beginning of every line of a file:

sed 's/^/MYTEXT /' path_to_file > path_to_file

Insert a text to the beginning of every stdout line:

COMMAND | sed 's/^/MYTEXT /'