LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-17-2007, 01:00 AM   #1
GSMD
Member
 
Registered: Dec 2005
Distribution: Gentoo
Posts: 87

Rep: Reputation: 16
Question removing a block of text with sed


I've got a script that creates multiple instances of a web-application, each with it's own url (e.g host.com/app1, host.com/app2).
What I do now, is create a separate "site" file for apache2 and then enable it (a2ensite under ubuntu). This works, but I eventually end up with a bunch of files in /etc/apache2/sites-available which is ugly.
So the concept is to have a single file for all of the projects with corresponding sections of it being added (no prob) and removed as a web-app gets removed.
Could anyone please share experience on how to make sed search for the pattern like
Code:
<-- web_app_name -->
....
<-- web_app_name -->
and then remove it and everything in-between?
Is it possible with sed at all? (can't imagine the way this could get done)

TIA

Last edited by GSMD; 04-17-2007 at 02:38 AM.
 
Old 04-17-2007, 01:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This deletes in-place, try without the -i option first:
sed -i '/<-- web_app_name -->/,/<-- web_app_name -->/d' infile

Hope this helps.
 
Old 04-17-2007, 01:46 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python, here's an alternative:
sample input:
Code:
<-- web_app_name -->
test
test
line
esfas
saf
<-- web_app_name -->
sfas
safdlka
ljsafd
kjasfd
<-- web_app_name -->
script:
Code:
data = open("file").readlines()
data = [i.strip() for i in data]
ind = []
for num,item in enumerate(data):
    if "<-- web_app_name -->" in item:
	ind.append(num)
for i in range(len(ind)):
    try:
	print '\n'.join(data[ ind[i]+1: ind[i+1] ])
    except: pass
    else: print
output:
Code:
#./test.py
test
test
line
esfas
saf


sfas
safdlka
ljsafd
kjasfd
 
Old 04-17-2007, 02:42 AM   #4
GSMD
Member
 
Registered: Dec 2005
Distribution: Gentoo
Posts: 87

Original Poster
Rep: Reputation: 16
druuna, thanks. this worked out perfectly.

ghostdog74, thanks. Python is what's I'm going to start learning really soon.
 
  


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
removing new line character using sed Fond_of_Opensource Linux - Newbie 4 03-26-2010 02:19 PM
help removing some text from a file (sed) BrianK Programming 2 08-13-2006 07:10 PM
removing text shadows in kde; removing taskbar color gradient webazoid Linux - Software 1 04-11-2006 02:56 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Removing <CR> using sed stv_t Programming 2 10-04-2003 04:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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