LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-14-2010, 04:34 AM   #1
ariszlo
LQ Newbie
 
Registered: Nov 2004
Location: Szeged, Hungary
Distribution: Yoper
Posts: 4

Rep: Reputation: 0
Find pattern and comment out 2 lines after it


How do yo find a pattern with sed or awk and then:
- add a # at the beginning of the line containing it
- and add a # at the beginning of the following 2 lines, too?

Say, I want to comment out the line containing "which 0launch" and the two lines following it:

if [ -x "`which 0launch`" ]; then
exec 0launch http://rox.sourceforge.net/2005/interfaces/ROX-Filer -S
fi

Expected result:

#if [ -x "`which 0launch`" ]; then
# exec 0launch http://rox.sourceforge.net/2005/interfaces/ROX-Filer -S
#fi

I need this because I do not want to comment out every line containing "fi", just the "fi" of this specific if statement.
 
Old 10-14-2010, 05:15 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Well I will assume you would know how to place a # at the front of the single line. To do the 2 following lines you can either use a counter or getline
 
Old 10-14-2010, 05:36 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
In sed you can use /<pattern>/ to find a match

sed '/which 0launch/,+2s/^/#/' file >newfile

Or
sed '/which 0launch/,/^fi/s/^#/' file >newfile
Which will work over a variable range of lines. I'm making an assumption that any subsequent if .. fi regions inside the region to comment will be indented.

Last edited by jschiwal; 10-14-2010 at 05:41 AM.
 
Old 10-14-2010, 06:37 AM   #4
ariszlo
LQ Newbie
 
Registered: Nov 2004
Location: Szeged, Hungary
Distribution: Yoper
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
sed '/which 0launch/,+2s/^/#/' file >newfile
sed '/which 0launch/,/^fi/s/^/#/' file >newfile
Thank you very much, both work fine.
 
  


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] sed: Find pattern and delete 5 lines after it supersoni3 Programming 4 03-24-2010 08:00 AM
search for a pattern and comment the line swaroop.tata Programming 2 09-04-2009 08:23 PM
Comment lines 1-9, 11-19, 21-29 using Sed kushalkoolwal Programming 8 02-03-2009 05:02 PM
listing non-comment lines scbops Linux - Newbie 5 09-18-2007 09:55 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM


All times are GMT -5. The time now is 09:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration