in reply to Searching for control characters

If I understand your task, you could just do it this way:
#!/usr/bin/perl use strict; use warnings; while (<>) { my @ctrl = ( /([[:ctrl:]])/g ); if ( @ctrl ) { my $cstr = join ",", map { sprintf "x%2.2x",ord() } @ctrl; s/([[:ctrl:]])/sprintf("||%x2.2x||",$1)/eg; printf( "line %d: ctrl-chars %s in <<%s>>\n", $., $cstr, $_ ); } }
Run it like this:
script_name memo.txt > memo.errs
It'll behave a little differently from your version: if a single line contains two or more control characters, this will show all of them (e.g. "x13,x01,x11"), instead of showing just the first one.

That could also be done as a one-liner (using "perl -lne"), but then you might have shell quoting issues that I don't want to get into...

(UPDATE: Added a line of code inside the "if(@ctrl)" block, to mark the locations in the data line where the control characters occurred, as per OP's stated intention.)

Replies are listed 'Best First'.
Re^2: Searching for control characters
by ericdp (Novice) on Feb 08, 2006 at 16:45 UTC
    hmmm... I try this and get:
    POSIX class :ctrl: unknown before HERE mark in regex m/([:ctrl: << HERE ])/
    
    Is my perl compiled differently to cause this error? I'm in a ksh88 environoment on HPUX.

    Thanks for help. This looks like a better idea.
    Eric
      Ah. supposed to be [[:cntrl:]] :)

      but now getting
      ...
      Argument "\n" isn't numeric in sprintf at ./a3.pl line 10, <> line 2326.
      ...
      
      so changed the sprintf to %s and ord $1. otherwise it can't seem to print ^I ^f type of characters.
      Thanks for the help

      Eric