LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-04-2011, 04:10 PM   #1
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Rep: Reputation: 16
Help sed delete before and after


Hello members,

I am stuck on a sed issue I can't seem to resolve. I am trying to get rid of lines before and after a regex match from the following dataset:

Code:
define host {
        host_name                       4000-somehost-app
        alias                           4000-somehost-app
        address                         192.168.2.30
        use                             4000-Host
}
define host {
        host_name                       4000-somehost2-app
        alias                           4000-somehost2-app
        address                         192.168.2.40
        use                             4000-Host
}

I have gotten this far but am stuck on how to proceed with removing additional lines in the beginning and a couple of lines forward:

Here is my attempt:

Code:
#!/bin/sh


sed -n '
        /define host/,/}/ {
                /4000-somehost-app/ D

                p
        }
'
My goal is to wipe out the "4000-somehost-app" and leave only "4000-somehost2-app". Can this be done with sed?


Any pointers on what I am doing wrong can be of great help.

Thanks
 
Old 01-04-2011, 05:50 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

since your code already keeps only two lines I am assuming that you actually want to keep the entire block which contains '4000-somehost2-app'. This will delete anything else:
Code:
sed -nr '/define host/ {:a N; /}/ ! ba; /4000-somehost2-app/ p}' file
This will delete only the block that contains '4000-somehost-app'
Code:
sed -r '/define host/ {:a N; /}/ ! ba; /4000-somehost-app/ d}' file
Not sure what exactly you want.
 
Old 01-04-2011, 06:40 PM   #3
tuxtutorials
Member
 
Registered: Dec 2008
Location: New York
Distribution: RedHat, Solaris
Posts: 68

Original Poster
Rep: Reputation: 16
Solved

Thanks crts,

Your line:

Code:
sed -r '/define host/ {:a N; /}/ ! ba; /4000-somehost-app/ d}' file
did it, I am going to go over this tomorrow and post back some questions regarding your approach.

Thanks again
 
  


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
Delete next line in sed Yalla-One Programming 9 01-04-2009 04:00 PM
sed and multiline delete aliyesami Programming 3 06-27-2008 02:03 PM
using sed to delete line to the right khairilthegreat Linux - Newbie 5 07-28-2007 02:10 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
cannot delete certain files with sed ganninu Linux - General 6 12-10-2003 03:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:50 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
Open Source Consulting | Domain Registration