Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,Perl Gurus,

I am trying to read the file names outputed by 'ls --color -p' in my perl script using a pipe.
So the complete command is 'ls --color -p | myPerl'.

But the file names read in have some very strange chars around them, which are operating system delimiters (linux redhat8). They are not regarded as white spaces, they are even patterned as printable chars. If I match them with POSIX' [[[:print:]]], they look like [00m. However, I can not find any pattern to remove them. It is so frustrating. Could you please give me some help on this? Thank you so much.

All the best. ginger

Replies are listed 'Best First'.
Re: remove operating system delimiters?
by jasonk (Parson) on Dec 05, 2003 at 00:10 UTC

    Those are ANSI color codes, the easiest way to get rid of them is to not use --color in your ls command, then you won't have them to worry about.


    We're not surrounded, we're in a target-rich environment!
Re: remove operating system delimiters?
by sauoq (Abbot) on Dec 05, 2003 at 02:21 UTC

    If you are having trouble because that's an alias you use, you can change your alias to use --color=auto instead. Then ls will try to the right thing at the right time.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: remove operating system delimiters?
by Roger (Parson) on Dec 05, 2003 at 00:56 UTC
    Not to recommend this, but just to prove that it could be done. The following script accepts input from ls --color -p on STDIN, and strip the ANSI color commands with regex. I guess it's the regex that you are interested in.

    use strict; binmode STDIN; # put STDIN in binary mode while (<STDIN>) { s/\033[^m]*m//g; # strip ansi color commands print $_; }
      Thank you so much for the magic. That works very fine.
      Do you happen to know the patterns for any other OS delimiters for Linux redhat8?
      I figured I need to prepare for all the possible inputs.
      Wish you a happy holiday. ginger
        Those aren't OS delimiters. They are ANSI escape codes for displaying color. If you remove --color option from the ls command, they won't show up. You could also use --color=auto which causes to ls to only send color codes when it is talking to a terminal.