LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Any way to split up STDOUT and STDERR, and then recombine in the right order? (https://www.linuxquestions.org/questions/linux-software-2/any-way-to-split-up-stdout-and-stderr-and-then-recombine-in-the-right-order-436041/)

Lumberg 04-17-2006 12:09 PM

Any way to split up STDOUT and STDERR, and then recombine in the right order?
 
OK, the question may be a bit misleading, so here is what I am trying to do:

I have a bunch of jobs in cron, and I would like all the output to eventually end up in a log file in exactly the order it would come out if run interactively.

However, I would like to somehow mark the lines that come out via STDERR so that I can say color them or something so I can pick them out more easily. This may be as simple as prepending some predetermined series of characters to all STDERR lines like @@@@@ or such.

Is this possible?

Thanks

Wells 04-17-2006 12:30 PM

I suppose you could pipe the STDERR (or STDOUT) output through sed, then push that into a file:

$ binary1 1> log.txt 2>&1 |(sed "s/^/STDERR:/" >> log.txt)

(I *think* this would work. I haven't tested it, and I really don't know if it will work properly. Give it a shot and see what happens, I guess...)

Lumberg 04-17-2006 01:02 PM

Hmm, doesn't seem to work. Perhaps with parentheses around the first part, thus:

(binary1 1> log.txt)

note the 1 before the > is superfluous in my limited experience.

Lumberg 04-17-2006 02:00 PM

not working yet, further suggestions welcomed. :)

Lumberg 04-17-2006 02:25 PM

OK, so

(find /lcl/fst/ -name "*ooo*" 1> log.txt) 2>&1 |(sed "s/^/STDERR:/" >> log.txt)


Does separate out STDERR and STDOUT, but it doesn't reassemble them in the right order; the STDERR: lines are all at the bottom of the file. I'd like all the lines to be in the same order as emitted.

Lumberg 04-17-2006 02:57 PM

(find /lcl/fst/ -name "*ooo*" >> log.txt) 2>&1 |(sed "s/^/STDERR:/" >> log.txt)

assembles the line in CLOSER to the expected order but still not quite.

Lumberg 04-17-2006 03:25 PM

Sounds like I need to somehow "un-buffer" stderr. By googling I see this is possible in Perl. Is it possible in a shell?

Lumberg 04-17-2006 03:57 PM

getting closer:

((find /lcl/fst/ -name "*ooo*") 3>&1 1>&2 2>&3 ) | sed 's/^/STDERR:/'

gives me what I want to the console; now to redirect the whole shebang into a file!

Lumberg 04-17-2006 03:59 PM

Nope, still the wrong order, but at least lines aren't getting interrupted.

Lumberg 04-17-2006 04:08 PM

OK, there HAS to be a way to do this, because I got the idea from emacs. In our implementation here, when you issue a command, stdout and stderr both go to the console, but stdout is the default font color, and the stderr stuff is another color. I just want to replicate this for an HTML page to view cron job status.

Lumberg 04-18-2006 08:38 AM

Anybody else with an idea?


All times are GMT -5. The time now is 04:33 PM.