Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Wanted: humanly readable `script` output

by pc88mxer (Vicar)
on May 04, 2008 at 19:55 UTC ( #684469=note: print w/replies, xml ) Need Help??

in reply to Wanted: humanly readable `script` output

If it is only backspaces you want to remove, try this:
my $text = "abcDE\x08\x08xyz\x08Z"; # \x08 is the backspace character while ($text =~ m/\x08/g) { substr($text, pos($text)-2, 2, ''); } print $text, "\n"; # emits: abcxyZ
Actually, I'm a little surprised that this works. I would think that modifying $text before the last match position in a m//g loop would screw things up.

Update: A slightly more optimized version:

while ($text =~ m/(\x08+)/g) { substr($text, $pos-2*length($1), 2*length($1), ''); } print $text, "\n";

Replies are listed 'Best First'.
Re^2: Wanted: humanly readable `script` output
by mrslopenk (Acolyte) on May 04, 2008 at 20:11 UTC
    Thanks for the suggestions everyone, but there are a lot of other control characters besides the backspace. If you are at a unix terminal, try this:
    script ls exit
    Then look in typescript. You'll see what I mean. Actually, there is a semi-passable solution that does not involve perl:
    TERM=dumb script ls exit cat typescript | col -b > readable.txt
    This seems to work as long as all the commands you type after script work on a dumb terminal. I was just wondering if perl had a more robust solution. I had a look at Term::Cap, but I fail to understand how to use it. I feel there must be a solution because, after all, I can see the very text I want on the terminal screen -- it's all right there! -- until my buffer size is reached. Usually copy/paste with the mouse from terminal to emacs works ok, but it didn't work the other night when I was compiling a kernel -- the output exceeded my buffer size.
      To capture the output of a kernel compile, why not just redirect the output?
      make zImage... > compile-output 2>&1
      Also, I usually put such commands in the background and tail the output file occasionally to see how things are going,
        Yes, that would have been a good idea. Still, I would like a transcript of both the commands I typed and the output.

        Moreover, it would make the perl-kiddie in me quite happy if I could parse terminal control codes in my favorite language.
      #!/usr/bin/perl use strict; use warnings; open(FILE,"<","typescript") || die; while (my $line=<FILE>) { $line=~s/.\x08//g; $line=~s/\p{IsC}//g; print "$line\n"; } close(FILE);
      This left behind a lot of funny things, which, to my eyes look like:
      [00m, [01;34m, [01;35m, [m]0;

        What is left looks like the colour control sequences for a fancy bash prompt. You might want to set the prompt to something humble with, say

        export PS1='\u@\h\$ '


        "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)

      This seems to work as long as all the commands you type after script work on a dumb terminal. I was just wondering if perl had a more robust solution

      How could removing terminal-specific control sequences be any more robust than not having them in the first place?

      The next best solution would be to have some program which process's your terminal's control sequences to generated a flat file. However, emulating a devices is rather complex and error-prone (hardly robust), and you'll have to make some concession or another in order to handle text being overwritten.

Re^2: Wanted: humanly readable `script` output
by moritz (Cardinal) on May 04, 2008 at 20:03 UTC
    You can replace the while loop with this little regex:
    $text =~ s/.\x08//g;

      Not really. Consider:

      my $text = "No \x08\x08\x08 good"; $text =~ s/.\x08//g; print $text;


      No good

      Perl is environmentally friendly - it saves trees

        This problem with the way /g works is fixed with the 1 while hack

        [james@adajio ~]$ cat #!/usr/bin/perl my $text = "No \x08\x08\x08 good"; 1 while $text =~ s/.\x08//; print "$text\n"; [james@adajio ~]$ ./ good [james@adajio ~]$

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://684469]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-11-29 08:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found