LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multiple substitutions in one expression using sed (https://www.linuxquestions.org/questions/programming-9/multiple-substitutions-in-one-expression-using-sed-4175505483/)

useretail 05-19-2014 03:14 PM

Multiple substitutions in one expression using sed
 
Hi, I'm trying to get multiple substitutions in one expression using sed:

Code:

echo "--_----__-___-_-__-" | sed -e "s/[_-|-_|--]/-/g"
So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash)

But I'm getting an errors.

grail 05-20-2014 01:34 AM

So you have marked the question as solved, does this mean you worked it out? If so, why not share your new found wisdom so others may benefit.

Also, if you are getting errors, it would be prudent to show the errors otherwise how do we know how to help?

useretail 05-20-2014 03:01 AM

Quote:

you have marked the question as solved
If you run sed with the multiple substitutions in one expression on the string that I provided you will receive:
Code:

-----__--_-
which means you doing only one pass with sed.

but if you run sed with three substitutions in separate expressions you will get another result:
Code:

----_--
which is more correct because you're doing three passes, but with different expressions.

grail 05-20-2014 09:59 AM

Well my first issue would be that you have posted what you started with and what solution you tried to use, but at no point do you show what your desired output should be?

I also have issues with the question as well as your proposed solution:

1. The question :- replace all instances of _-, -_, -- with - (dash) ... my issue here is, should the solution be recursive, ie based on the first 3 characters of your example '--_', now if recursive
the solution would first replace the first 2 dashes with a single dash and then on the next pass it will change the now single dash and an underscore to be a single dash, hence these 3 characters
would become simply ... '-' ... is this correct?

2. Proposed solution :- [_-|-_|--] ... this part of your regex says, look for a dash, a pipe or an underscore ... the fact that you have used patterns is irrelevant as the square brackets have indicated
you will show a group of individual characters or a group, such a-z, where any one of those characters can be replaced by your dash. You may have meant something more akin to:
Code:

sed -r 's/--|-_|_-/-/g'
Again this solution will not provide the recursive option spoken of above.


So as said initially, if you show us the desired output it can greatly help us to help you in coming up with a solution.

mina86 05-20-2014 03:22 PM

Quote:

Originally Posted by grail (Post 5174190)
2. Proposed solution :- [_-|-_|--] ... this part of your regex says, look for a dash, a pipe or an underscore ...

Actually, it does not. It's just incorrect group since dashes indicate ranges.

Quote:

Originally Posted by grail (Post 5174190)
Code:

sed -r 's/--|-_|_-/-/g'

For the recursive substitutions, “:” and “t” sed commands need to be combined with the above.

useretail 05-21-2014 05:20 PM

Quote:

if you show us the desired output
The problem is that there was no desired output - I made up that example. I was looking for general solution.

grail 05-22-2014 05:50 AM

If you do not know what you want as the output I can see no logical solution. As I have pointed out there are multiple options depending on the output you want.


All times are GMT -5. The time now is 06:16 PM.