Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

what regular expression do I need?

by Anonymous Monk
on Apr 18, 2013 at 09:47 UTC ( #1029300=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
Assuming that you have a file with lots of lines, that you require to contain ONLY the characters I, M and O, what is the regexp to use in order to find lines that contain other characters than I,M and O?
Thanks

Comment on what regular expression do I need?
Re: what regular expression do I need?
by kcott (Abbot) on Apr 18, 2013 at 09:54 UTC
Re: what regular expression do I need?
by hdb (Parson) on Apr 18, 2013 at 09:54 UTC

      Also, don't forget to "chomp" your lines, otherwise all lines will match...

Re: what regular expression do I need?
by LanX (Canon) on Apr 18, 2013 at 09:54 UTC
    Assuming you know how to read a file line by line:

     print "$. : $line\n" if $line =~ m/[^IMO]/;

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: what regular expression do I need?
by bimleshsharma (Beadle) on Apr 18, 2013 at 09:56 UTC

    Try this..

    while(<FILE>) { if($_ =~ /I/ || $_ =~ /M/ || $_ =~ /O/){} else { print "\n $_"; } }
Re: what regular expression do I need?
by MidLifeXis (Prior) on Apr 18, 2013 at 13:51 UTC

    Look at the documentation for perlre, chomp, and perlretut. Others have given you actual solutions to your (very homeworkish) question.

    --MidLifeXis

Re: what regular expression do I need?
by davido (Archbishop) on Apr 18, 2013 at 15:54 UTC

    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:

    $ ./nonimo.pl 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.


    Dave

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-07-23 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (134 votes), past polls