LinuxQuestions.org
Register a domain and help support LQ
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-28-2009, 09:07 AM   #1
masterdam79
LQ Newbie
 
Registered: Feb 2008
Posts: 8

Rep: Reputation: 0
SED multiple lines


Hiya,

I'm trying to so something simple, but everything related in previous posts on different forums seem overcomplicated.

What do I want to do:

I have a file (/home/richard/.qmail) which contains:

Code:
| /usr/local/psa/bin/psa-spamc accept
| true
And I have a script (/home/richard/script.sh) which contains:

Code:
SEARCH="| \/usr\/local\/psa\/bin\/psa-spamc accept\n"

sed "s/$SEARCH//g" /home/richard/.qmail > /home/richard/.qmail2
My expected output (/home/richard/.qmail2) would be:

Code:
| true
In other words, I want to use sed to find and replace the text

Code:
| /usr/local/psa/bin/psa-spamc accept
INCLUDING the LINE BREAK and basically delete the whole line, so that line 2 would shift up to line 1.

Any help MUCH appreciated, thanks in advance!

Richard

Last edited by masterdam79; 10-28-2009 at 09:11 AM. Reason: minor edits
 
Old 10-28-2009, 09:43 AM   #2
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
If you dont mind using 'vi' or 'ex' following will solve your problem:
Code:
(echo "g/SEARCH_PATTERN/d"; echo "wq OUT_FILE") | ex -s IN_FILE
I think the code is self explanatory.

Last edited by vijay_babu1981; 10-28-2009 at 09:55 AM. Reason: better form for writing to new output file
 
Old 10-28-2009, 09:48 AM   #3
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Or with sed:

Code:
sed "/SEARCH_PATTERN/d" INPUT_FILENAME > OUTPUT_FILENAME
 
Old 10-28-2009, 09:44 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You might also be pleased to know that you can use a different separator eg ':' in the sed cmd, when the search string contains eg '/' ie

sed "s:/some/path/here/::g" blah....

This avoids LTPS = Leaning Tooth Pick Syndrome. sed takes the char appearing in the first separator position to be the separator throughout.
 
Old 10-29-2009, 04:46 AM   #5
masterdam79
LQ Newbie
 
Registered: Feb 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hii thanks load guys!!!
Gonna try it today, will let you know!
 
Old 10-29-2009, 05:24 AM   #6
masterdam79
LQ Newbie
 
Registered: Feb 2008
Posts: 8

Original Poster
Rep: Reputation: 0
This is the command I'm gonna run or put in a script..

find /var/qmail/mailnames/ -name '.qmail' -exec sed -i "/psa-spamc/d" {} \ ;

easier is not possible.

Thanks a lot guys
 
Old 10-29-2009, 05:25 AM   #7
masterdam79
LQ Newbie
 
Registered: Feb 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
You might also be pleased to know that you can use a different separator eg ':' in the sed cmd, when the search string contains eg '/' ie
Yea knew that, is great!!
I used @ sometimes as seperator.
 
  


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
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 06:21 PM
Problems defining SED pattern over multiple lines dj_bridges Programming 12 10-31-2007 09:06 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 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
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 08:40 AM


All times are GMT -5. The time now is 02:44 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