Removing unwanted symbols with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Removing unwanted symbols with sed
# 8  
Old 02-10-2019
@nezabudka--I got your first code to work fine; I don't know how to employ the parameter thing

To all--I made this up myself, and it works! I know it's not elegant!
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g' | sed 's/*//g' | sed 's/,/, /g'

I'm assuming the double slashes before the "g" are for when you aren't substituting anything.
This User Gave Thanks to Xubuntu56 For This Post:
# 9  
Old 02-10-2019
Quote:
Originally Posted by Xubuntu56
...I don't know how to employ the parameter thing
In command line
Code:
set -- "blue:,*green:,*red:,*yellow"
echo $1
blue:,*green:,*red:,*yellow
echo ${1//:,\*/, }
>>> blue, green, red, yellow

In the script
Code:
cat script.sh
#!/bin/bash
echo ${1//:,\*/, }

./script.sh "blue:,*green:,*red:,*yellow"
>>> blue, green, red, yellow

In the script from function
Code:
cat script.sh
#!/bin/bash
myfunc() {
    echo ${1//:,\*/, }
}
myfunc "blue:,*green:,*red:,*yellow"

./script.sh
>>> blue, green, red, yellow


Last edited by nezabudka; 02-10-2019 at 04:36 PM..
This User Gave Thanks to nezabudka For This Post:
# 10  
Old 02-10-2019
Quote:
Originally Posted by Xubuntu56
...
To all--I made this up myself, and it works! I know it's not elegant!
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g' | sed 's/*//g' | sed 's/,/, /g'

I'm assuming the double slashes before the "g" are for when you aren't substituting anything.
Not talking of elegance, but efficiency: Above creates three processes to execute three instances of the sed command to achieve what can be done with a single one only. Scrutinizer already showed you how to combine several sed commands into the "first non-option argument" (c.f. man sed); you also can use several -e (expression) options on the command line:
Code:
echo "..." | sed -e's/://g' -e 's/*//g' -e 's/,/, /g'

And yes,the "double slashes" enclose the "empty string" for the replacement thus effectively removing the search pattern match.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed command for removing symbols

Hi I am trying to remove all the symbols in a file (testfile1) and using the below command to do that. Its not working for me. Can you help me on what is wrong with below script? I want to retain only alphabets in a file and remove all the symbols like *:.+= etc sed 's/^.//g' testfile1 > testfile2 (4 Replies)
Discussion started by: sandeepcm
4 Replies

2. Shell Programming and Scripting

Removing extra unwanted spaces

hi, i need to remove the extra spaces in the 2nd field. Sample: abc|bd |bkd123 .. 1space abc|badf |bakdsf123 .. 2space abc|bqe |bakuowe .. 3space Output: abc|bd|bkd123 abc|badf|bakdsf123 abc|bqe|bakuowe i used the following command, (9 Replies)
Discussion started by: anshaa
9 Replies

3. Shell Programming and Scripting

Removing extra unwanted spaces

hi, i need to remove the extra spaces in the filed. Sample: abc~bd ~bkd123 .. 1space abc~badf ~bakdsf123 .. 2space abc~bqed ~bakuowe .. 3space output: abc~bd ~bkd123 .. 1space abc~badf~bakdsf123 .. 2space abc~bqed~bakuowe .. 3space i used the following command, (2 Replies)
Discussion started by: anshaa
2 Replies

4. Shell Programming and Scripting

removing the words with symbols in a file in unix

I have file like below Hi iam author <br>joseph</br> in france. I live in my home <br></br> but no food. I will play footbal <br></br> but i wont play cricket. I will read all the books <br>all fiction stories</br> i hate horror stories. I want output like below Hi iam author... (3 Replies)
Discussion started by: vinothsekark
3 Replies

5. Shell Programming and Scripting

Removing unwanted tags from xml file

I have a XML file given as below: "<ProductUOMAlternativeDetails> <removetag> <UOMCode>EA</UOMCode> <numeratorForConversionToBaseUOM>1</numeratorForConversionToBaseUOM> <denominatorForConversionToBaseUOM>1</denominatorForConversionToBaseUOM> <length>0.59</length> <width>0.96</width> ... (3 Replies)
Discussion started by: vikingh
3 Replies

6. Shell Programming and Scripting

Removing Symbols From a File like the copyright symbol

Hi guys, I have a txt file full of funny symbols like the copyright symbol and other funny ones that get in the way when trying to use sed. For example, not sure if you can read this but I have a line that looks like this: 24(9):995Â*1001 DOI: 10.1007/s11606-009-1053-2 © When I'm using... (1 Reply)
Discussion started by: joshdg
1 Replies

7. Shell Programming and Scripting

removing unwanted characters from a file

i have a file like this 1111_2222#$#$dudgfdk 11111111_343434#$#$334 1111_22222#43445667 i want to remove all those charachetrs from # how can i do this Thank in advance Saravanan (4 Replies)
Discussion started by: saravanan71184
4 Replies

8. Shell Programming and Scripting

remove accents and symbols with sed

Hi, I would like to know how could I remove accentes and the symbols: º and ª of a text file with sed. Whis this command doesn't works :-( sed "s/í/i/g" filename Many thanks and sorry for my english! (7 Replies)
Discussion started by: mierdatuti
7 Replies

9. Shell Programming and Scripting

How to replace symbols by position using SED

Hi all, I need your help. For example I have string in file.txt: -x -a /tmp/dbarchive_NSS_20081204 -f 900 -l 1 2008/12/04 2008/12/04 So, I need to replace symbols from (for e.g.) position 26 till 33 with symbols which I have in file replace.txt And I have no idea how to do it. If... (1 Reply)
Discussion started by: nypreH
1 Replies

10. Shell Programming and Scripting

new lines symbols in sed?

I want to edit a huge script file using sed. How can I add the new lines symbols in the red colored places? sed -e "s/test -z "$x"/if test -z "$x" then echo -1; \n else \n/g" (1 Reply)
Discussion started by: gogogo
1 Replies
Login or Register to Ask a Question