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

Hello, I'm new to Perl. I'm trying to modify Perl code that used fgrep to remove all lines containing "intron", however, it also removes all lines that contain "splice_region&intron" which I would like to keep. How do I modify the code so it only removes lines with “intron” but keeps the lines “splice_region&intron”? Here is the exact Perl code that I'm trying to change:  my $cmd101 = `sed 's/Failed_amplicons\\([^@]*\\)@.@.@.@.@.@/|\\1@\\t@\\t@\\t@\\t@\\t@\\t@\\t@\\t@\\t@\\t@\\t#|\\1/g' $TOTALpfile |fgrep -v intron | fgrep -v non-preferred_transcript > $TOTALffile`; print $cmd101; Any help would be greatly appreciated, Mila

Replies are listed 'Best First'.
Re: remove lines with exact match only (OT)
by LanX (Saint) on Apr 04, 2016 at 19:09 UTC
    In Perl regex you can mark ("anchor") beginning and end of line with ^ and $.

    But the (badly formated) code you show doesn't look like perl

    Update

    Fgrep has nothing in common with perl, marked Off Topic

    Maybe have a look at Re: perl grep clone (CLI & perlrun) if you want to filter in a pipe using perl

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: remove lines with exact match only
by Marshall (Canon) on Apr 04, 2016 at 19:15 UTC
    More complicated regexes are possible if there are other formulations of XXX&intron that you want preserved.
    #!/usr/bin/perl use warnings; use strict; print grep{!/intron/ unless /splice_region&intron/}<DATA>; #Update: the English explanation: # exclude lines with "intron" except if that line # contains "splice_region&intron". =Prints: whehehe shshshs hshshsh #should pass thru som splice_region&intron intron 98797 #should pass wqer splice_region&intron poiy #should pass =cut __DATA__ something intron asdfasdf #should be deleted whehehe shshshs hshshsh #should pass thru som splice_region&intron intron 98797 #should pass wqer splice_region&intron poiy #should pass
Re: remove lines with exact match only
by GotToBTru (Prior) on Apr 06, 2016 at 16:23 UTC

    This is not "Perl code that used fgrep". This is (sed and) fgrep executed by Perl, which is a step backward. Put the commands in a shell script.

    Everything sed and fgrep do can be done in Perl. If that is your goal, check out the Tutorials here. Then you can try to convert the sed and fgrep commands into Perl. Post questions here if you get stuck. Note there are also tutorials on the web about converting sed to Perl (and vice versa).

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)