How to convert script command output into plain text
If you checkout the:
man script | grep -i bugs -A 2
You can see that:
script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.
What I came up with is a combination of tr
and sed
to get what we want.
Most of those characters are indicating colors, to remove most of them we can use this sed
command from here:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"
However it's still not enough... because as we saw in script's man
there are other not printable characters we should get rid of. To remove those we can simply use:
tr -dc '[[:print:]]\n'
Now we are almost there, the other problem in my test was that prompt being repeated twice, so to remove that start script
with dump
and TERM
's value TERM=dump
.
Final result:
-
To capture commands:
TERM=dump script my_output
-
To remove unnecessary characters:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" my_output | tr -dc '[[:print:]]\n' > new_output
For second part of your question, the output file is just a text file so we can't change the font or size of specific lines of it, however your output contains your prompt so you'll know which line is the command and which one is the output.
Related videos on Youtube
GD- Ganesh Deshmukh
These questions/answers on StackOverflow were done in last year, during college time, now I have improved a lot than last year. Backend Developer at Jio-Haptik. My Portfolio's Link: https://ganeshdeshmukh.me
Updated on September 18, 2022Comments
-
GD- Ganesh Deshmukh over 1 year
I am using
script-command
to save the output of terminal to file.but, it gives some extra characters like
square-like
characters and other text encrypted characters.man script
command tells, by using-q
flag/option, it gives quite an output, without writing start and done messages to standard output, but it won't remove extra characters.how can I get following:
1) make output file exactly same as of terminal output, removing extra characters.
2) In generated file, due to plain text-without color(unlike terminal), I can't figure-out directly
input commands
, because output and command I entered appears of same font,can I make command as bold or easily recognizable than rest output?
-
GD- Ganesh Deshmukh over 5 yearsthanks, but only small doubt is left, still
]0;
is appearing beforeterminals username
. screenshot here imgur.com/a/MWPpBLb -
Ravexina over 5 yearsIf you use
TERM=dump script
to runscript
it shouldn't appears anymore... yo remove]0;
you can usesed 's/^]0;//' output
it should do the job. -
GD- Ganesh Deshmukh over 5 yearsthanks, but still
sed 's/^]0;//' filename
didn't work. I also triedsed 's/^]0;//g'
. I didn't usedTERM=dump script
as I am appending my output to previously created file, and I am usingscript command
. -
Ravexina over 5 yearsuse
TERM=dump script -a
to append...