Results 1 to 4 of 4
I might have the wrong forum here, but regardless i'm going to ask here first.
I want to replace a character with sed under the condition that a specific character ...
- 08-27-2007 #1Just Joined!
- Join Date
- Aug 2007
- Posts
- 2
Sed Question
I might have the wrong forum here, but regardless i'm going to ask here first.
I want to replace a character with sed under the condition that a specific character doesn't follow.
Eg: i want to replace '+' but only when '+' is not followed by another '+' ('++')
The problem is i can't use s/\+[^\+]/ /g because it will replace the second character if it doesn't fail. Is there a method to either have it put back the second charactor or search for this case, but only replace the first character?
Another alternative would be to have it replace the first of the two '+'s but not the second. The problem is i need to use the (g)lobal option so that i replace cases after that.
An example string would be: This+is+a+string+with+a+following++
Result: This is a string with a following +
I'm sure this is hard to read, and for that i'm sorry but i'm really not sure how to best explain this.
- 08-27-2007 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
How about this:
sed -e 's/++/##/g' -e 's/+/ /g' -e 's/##/ +/g'
It changes the '++' to '##' so the next substitution '+' to ' ' won't pick them up. The last substition replaces the '##' to ' +'.
Vic
- 08-27-2007 #3Just Joined!
- Join Date
- Aug 2007
- Posts
- 2
I had a similiar thought but since i don't control the input i could potentially end up replacing something the user had entered. I suppose though the longer and more unique the characters the less likly this is that you'll replace the user's input.
Thanks, i'll use that in the mean time, instead perhaps using $ since i can't think of an instance where I would see $$. Any other thoughts.
I looked at the man pages but couldn't decipher the section that looked interesting. It had something to do with &, \1 \2 and setting markers and such.
- 08-27-2007 #4Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
welcome to the forum
very clear description--an example always helps. A crude solution
employs a placeholder for the "++" thus:
sed 's/'\+\+'/GGGGGG/g;s/+/ /g;s/GGGGGG/ +/g' < test
where "test" is your example.the sun is new every day (heraclitus)


Reply With Quote