LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


Hi
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, 01:50 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

Replace `s///' by `s@@@':
Code:
/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:
Code:
/usr/bin/perl -i -p -e "s@.*hourly.*@@g" crontab
to remove
Code:
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

Last edited by firstfire; 02-14-2012 at 01:59 PM.
 
Old 02-14-2012, 03:55 PM   #3
Cedrik
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 ')
Code:
perl -i -ne 'print unless m@/dir1/dir2/dir3/filename -q@' crontab
 
  


Reply


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 12:33 PM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 06:09 PM
Perl command to replace string by line numbe andiramesh_m Linux - General 6 07-15-2010 01: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 07:35 AM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 03:27 PM


All times are GMT -5. The time now is 01:56 PM.

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