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

Re: what regular expression do I need?

by davido (Archbishop)
on Apr 18, 2013 at 15:54 UTC ( #1029383=note: print w/replies, xml ) Need Help??

in reply to what regular expression do I need?

A simple regex works fine, but transliteration is often more efficient. Here's an approach with tr///. This snippet iterates over each line of each file supplied at the command prompt, and prints the filename and line number, along with a highlighted version of the line's data showing where the problems are:

while( <> ) { chomp; if( tr/IMO//c ) { s/([^IMO]+)/[-->$1<--]/g; print "File: $ARGV, Line $.: $_\n"; } }

This would be invoked as:

$ ./ filename1.txt filename2.txt

It could be expressed as a one-liner, in fact, like this:

perl -lnE 'if(tr/IMO//c){s/([^IMO]+)/[-->$1<--]/g; say "$ARGV-$.: $_"} +' file1.txt file2.txt fileXX.txt

The output is a little more terse, but it does the same thing. In either case, if your objective is just to print the bad lines, you can easily simplify the print statement.


Replies are listed 'Best First'.
Re^2: what regular expression do I need?
by tbone654 (Beadle) on Apr 19, 2013 at 04:33 UTC
    How about...
    cat file1 | perl -e 'while(<>){ next if !/IMO/; print $_;}'
    If the lines can only begin with IMO then:
    cat file1 | perl -e 'while(<>){ next if !/\bIMO/; print $_;}'
    If case is not important then:
    cat file1 | perl -e 'while(<>){ next if !/\bIMO/i; print $_;}'
    If you have multiple words to search for, then create another file named file2 and enter the search words:
    vi file2 apple orange banana grep -f file2 file1
    From the unix command-line... file2 is the list of words to search for from lines in file1... Note: you may have to run "dos2unix file2" to remove LF/CR or use chomp somehow...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029383]
[markong]: " Perl: the Markov chain saw" :D aha
[Discipulus]: thanks 1nickt but seems i'm not able to use it correctly...
[Discipulus]: Eily i'm testing a failure: i ok if i get bad results.. perhaps there is another way to do it...
[Discipulus]: in other words: i have a contructor that carps on missing arguments and I want to test that it fails correctly, without carping out in the test output

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-01-22 11:28 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.