Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-14-2012, 12:31 PM   #1
Sir Todd
LQ Newbie
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
Perl Find and replace line in crontab

I am looking for a way to remove a line from crontab using perl.
This is the one liner that I am doing:
/usr/bin/perl -i -p -e "s/0 6 * * 0 /dir1/dir2/dir3/filename -q >/dev/null 2>&1//g" /crontab

but I am getting this sytax error.

Bareword found where operator expected at -e line 1, near "/dir3/filename"
(Missing operator before filename?)
syntax error at -e line 1, near "s/0 6 * * 0 /dir1/dir2"
Search pattern not terminated at -e line 1.

How do i modify this line to remove this line?
Old 02-14-2012, 12:50 PM   #2
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 704

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

Replace `s///' by `s@@@':
/usr/bin/perl -i -p -e "s@0 6 * * 0 /dir1/dir2/dir3/filename -q >/dev/null 2>&1@@g" /crontab
Slashes in path name conflict with s///. Alternatively you can escape all slashes in path name: "\/dir1\/dir2...".

EDIT: Also note that some symbols, such as `*', have special meaning inside regular expressions, so you should esape them if you need literals: `\*' etc.

I'd recommend you to use a unique substring of the string you want to delete, for example:
/usr/bin/perl -i -p -e "s@.*hourly.*@@g" crontab
to remove
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

Last edited by firstfire; 02-14-2012 at 12:59 PM.
Old 02-14-2012, 02:55 PM   #3
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Another way to do it (will remove complete line)
(assuming there is only one line containing ' /dir1/dir2/dir3/filename -q ')
perl -i -ne 'print unless m@/dir1/dir2/dir3/filename -q@' crontab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find text in a line, Replace line Cpare AIX 3 04-01-2011 11:33 AM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 05:09 PM
Perl command to replace string by line numbe andiramesh_m Linux - General 6 07-15-2010 12:00 PM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration