LinuxQuestions.org
Visit Jeremy's Blog.
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 05-19-2014, 03:14 PM   #1
useretail
LQ Newbie
 
Registered: May 2014
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 05-20-2014, 01:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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?
 
Old 05-20-2014, 03:01 AM   #3
useretail
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.

Last edited by useretail; 05-20-2014 at 03:11 AM.
 
Old 05-20-2014, 09:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
1 members found this post helpful.
Old 05-20-2014, 03:22 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by grail View Post
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 View Post
Code:
sed -r 's/--|-_|_-/-/g'
For the recursive substitutions, “:” and “t” sed commands need to be combined with the above.
 
Old 05-21-2014, 05:20 PM   #6
useretail
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-22-2014, 05:50 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
1 members found this post helpful.
  


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
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
perl- multiple substitutions on same line gnashley Programming 2 11-12-2007 08:29 AM
variables within sed substitutions? ocicat Programming 3 07-29-2007 12:17 PM
sed? -- making substitutions scabies Linux - Software 1 12-21-2003 11:39 PM
Case sensitivity in SED substitutions jkoch Programming 1 02-21-2003 04:15 PM

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

All times are GMT -5. The time now is 03:30 AM.

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