I have an output file (namely a log from screen
) containing several control characters. Inside the screen, I have programs running that use control characters to refresh certain lines (examples would be top
or anything printing progress bars).
I would like to output a tail
of this file using PHP. If I simply read in that file and echo its contents (either using PHP functions or through calling tail
, the output is messy and much more than these last lines as it also includes things that have been overwritten. If I instead run tail
in the command line, it returns just what I want because the terminal evaluates the control characters.
So my question is: Is there a way to evaluate the control characters, getting the output that a terminal would show me, in a way that I could then use elsewhere (e.g., write to a file)?
@5gon12eder's answer got rid of some control characters (thanks for that!) but it did not handle the carriage return part that was even more important to me.
I figured out that I could just delete anything from the beginning of a line to the last carriage return inside that line and simply keep everything after that, so here is my sed command accomplishing that:
sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g'
The output can then be further cleaned using @5gon12eder's answer:
cat screenlog.0 | sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g' | sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g'
Combined, this looks exactly like I wanted.