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

How do I remove control characters?

by Grunk (Initiate)
on May 07, 2002 at 21:30 UTC ( [id://164831]=perlquestion: print w/replies, xml ) Need Help??

Grunk has asked for the wisdom of the Perl Monks concerning the following question: (regular expressions)

I have a korn shell at work that strips all the control characters (octal 041-176). I'm trying to write a job in perl to do the same to do some speed comparisons.

My question is, how do I specify to sub a range of octal values to a space (default)?
something like
s/!(octal value 041-176)/ /g

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I remove control characters?
by jsprat (Curate) on May 07, 2002 at 22:28 UTC
    Try perldoc perlop and look for the transliteration operator.

    tr/\040-\176/ /c;
    The 'c' option means complement the range.
    Note: you may have to binmode the filehandle, depending on your OS.
Re: How do I remove control characters?
by tachyon (Chancellor) on May 08, 2002 at 08:52 UTC

    tr/// is better but all you needed to make your pseudocode real perl code was:

    s/[^\040-\176]/ /g

    Note that the control chars are \000 - \037 so these are perhaps more direct:

    tr/\000-\037/ /; # best option for speed s/[\000-\037]/ /g # but this still works

    Note chr 32 dec 040 octal is the space so you were double spacing your spaces by starting at \041

Re: How do I remove control characters?
by kgsz (Initiate) on Jan 09, 2005 at 15:37 UTC

    For similiar problem (removing colors):

    For shell:

    cat something | perl -le 'while (<>) {chomp; s/[\000-\037]\[(\d|;)+m// +g; print ($_) };'

    In generic:
    # sth; sth; { s/[\000-\037]\[(\d|;)+m//g; } # sth else;
    ANSI colors looks like :) ^[[number[;number]m - reset: ^[[m
Re: How do I remove control characters?
by Grunk (Initiate) on May 07, 2002 at 21:31 UTC
    Oops, I mean to strip off all the control characters and leave only the characters (041-176).
    sorry

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found