Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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";


Comment on Re: Wanted: humanly readable `script` output
Select or Download Code
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;

      Prints:

      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 test.pl #!/usr/bin/perl my $text = "No \x08\x08\x08 good"; 1 while $text =~ s/.\x08//; print "$text\n"; [james@adajio ~]$ ./test.pl good [james@adajio ~]$
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.
      #!/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;
      characters.

        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\$ '

        loris


        "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)
      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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-09-20 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls