Removing unwanted symbols with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Removing unwanted symbols with sed
# 1  
Old 02-09-2019
Removing unwanted symbols with sed

I would like produce

Code:
blue, green, red, yellow

from
Code:
"blue:,*green:,*red:,*yellow

I can remove the colon with
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g'

which gives
Code:
blue,*green,*red,*yellow

but when I try
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g'; 's/*//g'

I get
Code:
bash: s/*//g: No such file or directory

distro Ubuntu 18.04.1 LTS; desktop Xfce 4.12.3 (Gtk 2.24.31); kernel 4.15.0-45-generic i686 Bash version 4.4.19(1)-release; Dell Inspiron-518
# 2  
Old 02-09-2019
Remove the single quotes in the middle:
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g; s/*//g'

or
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/[:*]//g'



--
The single quotes turned it into two commands:
One was:
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g'

and the other was:
Code:
's/*//g'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-09-2019
How about (untested as I'm not at my home computer):


Code:
sed 's/^[^[:lower:]]*\|[^[:lower:]]*$//g; s/[^[:lower:]]*/, /g' file

or
Code:
sed 's/^[^[:lower:]]*\|[^[:lower:]]*$//g; s/[^[:lower:]]+/, /g' file

(possible the + needs to be escaped; can't test...)
This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-09-2019
Gentlemen, thank you!
@Scrutinizer--your suggestions worked very well; I just need to figure out how to put spaces after the commas.
@RudiC--your second suggestion worked perfectly after escaping the "+". The first code produced a very interesting result!
Code:
, b, l, u, e, g, r, e, e, n, r, e, d, y, e, l, l, o, w,

# 5  
Old 02-09-2019
Quote:
Originally Posted by Xubuntu56
Gentlemen, thank you!
@Scrutinizer--your suggestions worked very well; I just need to figure out how to put spaces after the commas.
[..]
In this case:
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/://g; s/*/ /g'

or use:
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/[:*]//g; s/,/, /g'



--
Note: \+ and \| are GNU extensions that will not work with regular sed
In regular sed, the equivalent would be something like:
Code:
sed 's/^[^[:lower:]]*//g; s/[^[:lower:]]*$//g; s/[^[:lower:]]\{1,\}/, /g'


Last edited by Scrutinizer; 02-09-2019 at 09:22 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-09-2019
@Scrutinizer--both perfect, thanks!
# 7  
Old 02-10-2019
Maybe I did not understand again. I reread discussion several times Smilie
Code:
echo "blue:,*green:,*red:,*yellow" | sed 's/:,\*/, /g'

--- Post updated at 14:35 ---

If this string in a function or in a script, example as first parameter
Code:
echo ${1//:,\*/, }

These 2 Users Gave Thanks to nezabudka 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