LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   removing a block of text with sed (https://www.linuxquestions.org/questions/programming-9/removing-a-block-of-text-with-sed-546658/)

GSMD 04-17-2007 01:00 AM

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

druuna 04-17-2007 01:05 AM

Hi,

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

Hope this helps.

ghostdog74 04-17-2007 01:46 AM

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


GSMD 04-17-2007 02:42 AM

druuna, thanks. this worked out perfectly.

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


All times are GMT -5. The time now is 01:47 AM.