Multiple substitutions in one expression using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiple substitutions in one expression using sed
# 1  
Old 05-19-2014
Multiple substitutions in one expression using sed

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

Code:
echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s/[_-|-_|--]/-/g"

So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash)

I have provided bad example. The question is how to use multiple substitutions in one expression. Post updated.

Last edited by useretail; 05-19-2014 at 05:27 PM.. Reason: updated question
# 2  
Old 05-19-2014
How about something simpler replace all _ with - This gives the result you want:
Code:
echo "--_----__-___-_-__-" | sed -e 's/_/-/g'

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-19-2014
The question is how to use multiple substitutions in one expression. I just provided bad example.
# 4  
Old 05-19-2014
Try removing the pipe (|) symbols from your sed expression.

Also, if the goal is to have just a line of dashes, you could just globally replace the underscore character.

Code:
echo "--_----__-___-_-__-" | sed 's|_|-|g'
-------------------

This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 05-19-2014
Standard sed cannot do it like that, you could use 3 separate passes, but it is not the same:
Code:
sed 's/_-/-/g; s/-_/-/g; s/--/-/g' file

But that would not give the same result, since it would also depend on the order of the substitution passes..

With extended regular expressions there you can use alternation, which is provided by BSD sed -E or GNU sed -r
Code:
sed -E 's/_-|-_|--/-/g' file

awk can do it standard:
Code:
awk '{gsub(/_-|-_|--/,"-")}1' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-19-2014
Scrutinizer
yeah, first command replaces to the desired result in three passes:
Code:
-foo-bar-foo-bar-foo_bar-foo-bar-

Is it possible to replace to that result using multiple substitutions in one expression?
# 7  
Old 05-19-2014
Yes. Use his awk solution. Most sed versions do not support ERE - what you want. awk does support it. I'm reasonably sure no other sed solution that is a single command and can be generalized with regular expressions. So, No. You cannot do what you ask with sed. Unless you use gnu sed or BSD sed.



ERE == extended regular expressions
This User Gave Thanks to jim mcnamara For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

2. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

3. Shell Programming and Scripting

Multiple Substitutions across Multiple Files

Hey everyone! I am determining the best method to do what the subject of this thread says. I only have pieces to the puzzle right now. Namely this: grep -rl "expression" . | xargs open (I should mention that the intention is to grep through many files containing the "expression" and... (2 Replies)
Discussion started by: Alexander4444
2 Replies

4. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

5. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

6. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

7. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies

8. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

9. Shell Programming and Scripting

need help on multiple expression

this is the content of the file: i want to remove both the line starting with "=" and "(" but i can only remove one at a time..so how do I go about removing both of them? (15 Replies)
Discussion started by: finalight
15 Replies

10. Shell Programming and Scripting

Expression for Finding Multiple Directories..??

I am writing a shell script to search for previous versions of an application...the application is called TAU and basically i want to search the users home directory and /Applications for any instances of a "TAU" folder.. If found i want to give the user the option to remove the old folders and if... (3 Replies)
Discussion started by: meskue
3 Replies
Login or Register to Ask a Question