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

line =08 * * 3 /data/reports/bin/xyz.ksh -o customreports.com -f abc.sql -m xyz.com -c -g

file:- abc.crontab

08 * * 3 /data/reports/bin/xyz.ksh -o customreports.com -f abc.sql -m xyz.com -c -g

06 * * 3 /data/reports/bin/xyz.ksh -o customreports.com -f abc.sql -m xyz.com -c -g

I have searched the line using following command

grep -F "08 * * 3" abc.crontab | grep "abc.sql"

I get the first line from file,I want to delete the exact matching line from abc.crontab

If I use sed -e '/abc.sql /d' abc.crontab/

$cmdperl ="perl -i -nle 'print if !/abc.sql/' abc.crontab ";

then both the lines get deleted from file. Please suggest me the solution.

Replies are listed 'Best First'.
Re: delete line from pattern matching
by davido (Cardinal) on Jun 18, 2012 at 16:49 UTC

    In an effort to minimize the changes to your existing strategy:

    $cmdperl = "perl -i -nle 'print if !/abc.sql/ || $found++;' abc.cronta +b";

    The first time that /abc.sql/ matches, the || logical short circuit will evaluate $found to be 'false', and thus no 'print will take place. However, because the $found++ expression was evaluated, $found will be incremented, making it "true". The next time /abc.sql/ matches, the || logical short circuit will evaluate $found as true, and will print. So the first time the "drop line" is found it gets skipped, and all other lines get printed.

    There may be more elegant (a Perl-afficionado's eyes) pure-Perl solutions, but sometimes the best solution is to just fix the line that needs fixing, and move on. ;)

    By the way: This is untested, so change the -i flag to -i.bak until you're sure it does what you want.


    Dave

Re: delete line from pattern matching
by 7stud (Deacon) on Jun 18, 2012 at 19:54 UTC

    I have searched the line using following command:

    grep -F "08 * * 3" abc.crontab | grep "abc.sql"

    I get the first line from file,I want to delete the exact matching line from abc.crontab

    How about this:

    perl -i -nle 'print if !(/08 [*] [*] 3/ and /abc.sql/)' abc.crontab