You are not logged in.

#1 2005-10-24 18:16:08

sven
Member
Registered: 2005-02-01
Posts: 311

[SOLVED]deleting text with sed

I would like to use sed to delete text that is between "anchor" tags, like this:

#startIncludeRules
the text
on
multiple lines
#endIncludeRules

How would that succeed using sed?

Offline

#2 2005-10-24 19:00:06

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

how's this for ugly? lol

i=(`grep -n IncludeRules name_of_file |awk -F: {'print $1'}`)
 i[0]=$((${i[0]}+1))
 i[1]=$((${i[1]}-1))
sed "${i[0]},${i[1]}"d -i name_of_file

Offline

#3 2005-10-24 20:35:20

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: [SOLVED]deleting text with sed

sed -e 's/^[^#].*//;/^$/d'

should remove every line that does not start with a #, maybe that's already enough?

Offline

#4 2005-10-25 11:18:11

sven
Member
Registered: 2005-02-01
Posts: 311

Re: [SOLVED]deleting text with sed

Thanks,Penguin and smoon smile

Removing # - lines is not enough,though - it is a configuration file that has other importan lines, too.

Penguin,your commands gave an error:
sed: -e expression #1, char 3: unexpected `,'

Offline

#5 2005-10-25 11:30:26

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

sven wrote:

Thanks,Penguin and smoon smile

Removing # - lines is not enough,though - it is a configuration file that has other importan lines, too.

Penguin,your commands gave an error:
sed: -e expression #1, char 3: unexpected `,'

I get that error to if I don't change "name_of_file" instances to the actual complete path to my file. Did you do that?

Offline

#6 2005-10-25 11:53:31

sven
Member
Registered: 2005-02-01
Posts: 311

Re: [SOLVED]deleting text with sed

yes, I tried now with full path, too - and still got that error

Offline

#7 2005-10-25 15:07:41

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

I can't figure out why that wont work for you. When I was testing it, I was using !#/bin/bash instead of !#/bin/sh.
What I gave you will grep any line with IncludeRules in it and delete everything between the two points. The first line may be failing. You could echo ${i[@]} after that first line to see if it actually did something and go from there.

Sorry, I can't think of an easier way to do this.

Offline

#8 2005-10-25 17:30:10

sven
Member
Registered: 2005-02-01
Posts: 311

Re: [SOLVED]deleting text with sed

First two lines seem to work nicely, the final value of $i is 2. echo ${i[@]} returns 2 -1.

Offline

#9 2005-10-25 18:33:49

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

yeah it looks like ifs finding the start tag but no the end tag. Could you post this file so I can get a better understanding of whats going on?

Offline

#10 2005-10-25 18:33:57

sven
Member
Registered: 2005-02-01
Posts: 311

Re: [SOLVED]deleting text with sed

I found another way big_smile I got help from a Gentoo sed Wiki

sed -e '/#startIncludeRules/,/#endIncludeRules/d' testfile | more > testfile

Offline

#11 2005-10-25 18:35:16

sven
Member
Registered: 2005-02-01
Posts: 311

Re: [SOLVED]deleting text with sed

My test file is this:

testing123
12
3
#startIncludeRules
fds
dsf
dsf
sf
sd
fs
dfdsfdsf
#endIncludeRules
sfds
fdsf
dsf
dsf
sd

Offline

#12 2005-10-25 18:36:30

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

ah. I thought you wanted to leave the tags but delete lines between them.

Offline

#13 2005-10-25 18:43:28

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [SOLVED]deleting text with sed

sven wrote:

My test file is this:

testing123
12
3
#startIncludeRules
fds
dsf
dsf
sf
sd
fs
dfdsfdsf
#endIncludeRules
sfds
fdsf
dsf
dsf
sd

even this worked for me  :?
Oh well, glad you got it figured.

Offline

Board footer

Powered by FluxBB