LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-07-2019, 10:47 PM   #1
Prasad321
LQ Newbie
 
Registered: Aug 2015
Posts: 5

Rep: Reputation: Disabled
Comment out a line using sed matching multiple patterns.


Below is the snippet of the file, I need to comment out 2 line which runs MON-SAT and the script run_abcd.sh.

10 5-23/2 * * SUN . ~/.bash_profile; $HOME/scripts/run_abcd.sh > $DIR/logs/run_abcd.log
10 1-23/2 * * MON,TUE,WED,THU,FRI,SAT . ~/.bash_profile; $HOME/scripts/run_abcd.sh > $DIR/logs/run_abcd.log
10 1-23/2 * * MON,TUE,WED,THU,FRI,SAT . ~/.bash_profile; $HOME/scripts/run_deposit.sh > $DIR/logs/run_deposit.log

using below sed I can comment out the script run_abcd.sh but it comments out both line1 and line2. I need only second line to be commented. Please advise.
sed -e '/run_abcd.sh/ s/^#*/#/' -i file_name
 
Old 08-07-2019, 11:13 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
That's easier under awk, but written in sed, you could add in a check. You are looking for lines with both strings, not just one or the others, so design your decision tree appropriately:

Code:
sed -i.bak -e '/ MON,TUE,WED,THU,FRI,SAT /{/run_abcd.sh/ s/^/#/}'
Also, only use the -i option with a value. That way a backup copy is kept automatically. The way you have it, a mistake will irreversably damage the orignal without leaving you an automatic backup.
 
1 members found this post helpful.
Old 08-07-2019, 11:15 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Prasad321 View Post
Below is the snippet of the file, I need to comment out 2 line which runs MON-SAT and the script run_abcd.sh.

10 5-23/2 * * SUN . ~/.bash_profile; $HOME/scripts/run_abcd.sh > $DIR/logs/run_abcd.log
10 1-23/2 * * MON,TUE,WED,THU,FRI,SAT . ~/.bash_profile; $HOME/scripts/run_abcd.sh > $DIR/logs/run_abcd.log
10 1-23/2 * * MON,TUE,WED,THU,FRI,SAT . ~/.bash_profile; $HOME/scripts/run_deposit.sh > $DIR/logs/run_deposit.log

using below sed I can comment out the script run_abcd.sh but it comments out both line1 and line2. I need only second line to be commented. Please advise.
sed -e '/run_abcd.sh/ s/^#*/#/' -i file_name
You need to comment out run_abcd.sh running Mon-Sat? If so, add Mon-Sat to the regular expression, for example
Code:
sed -i '/MON.*SAT.*abcd.sh/....

Last edited by berndbausch; 08-08-2019 at 12:41 AM.
 
1 members found this post helpful.
  


Reply



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
SED one-liner for remove or comment out a line matching a certain pattern after the first line match Honest Abe Linux - Newbie 8 10-04-2018 05:36 AM
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 07:41 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:11 AM.

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
Open Source Consulting | Domain Registration