Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file with lines as follows:

...
... <230948203[234]>, ...
... <234[24]>, ...
..

I would like to use sed to remove the characters < , and > from every line

I tried using sed 's/<>,//g' but it didnt work (it didn't change anything). Do I need to escape these special characters. Is it possible to delete multiple characters using a single sed command?

share|improve this question
2  
bash-3.2$ echo "<230948203[234]>," | tr '<>,' ' ' --> 230948203[234] -- EDITED Thanks to Paul –  user14039 Feb 29 '12 at 22:23
3  
@srikanthradix: That doesn't remove those characters, is replaces them with spaces. You want tr -d '<>,' '' (as in Chris Down's answer). –  Keith Thompson Feb 29 '12 at 23:23
1  
@KeithThompson: tr -d '<>,', without '' in the end, not? –  user unknown Mar 1 '12 at 22:46
    
@userunknown: Yes, thanks for the correction. –  Keith Thompson Mar 1 '12 at 22:47

2 Answers 2

up vote 9 down vote accepted

With sed:

sed 's|[<>,]||g'

With tr:

tr -d '<>,'
share|improve this answer
2  
Correct, but I personally find the use of | as a delimiter a bit confusing. sed 's/[<>]//g' is a bit easier to read. –  Keith Thompson Feb 29 '12 at 23:21
    
@KeithThompson I use it for practicality. I find myself dealing with literal | (and thus having to escape it) much less than I have to deal with literal /. –  Chris Down Feb 29 '12 at 23:34
2  
A fair point. On the other hand, | is also often a meta-character, used for alternation in some regexp syntaxes (though sed in particular needs \| for that). Personally, if I need to deal with literal / characters, I usually use , as the delimiter. –  Keith Thompson Mar 1 '12 at 0:38
1  
Aside from the personal/specific situations mentioned above, there is one intrinsic syntax difference when using a non-standard delimiter. The first non-/ delimiter must be escaped for each sed range expression, eg: printf 'a\nb\nc\n' | sed -n '\|a|,\|b|p' –  Peter.O Mar 2 '12 at 14:40

Try this one: sed 's/[<>,]//g'

share|improve this answer
    
What are the vertical bars there for? –  Paul Tomblin Feb 29 '12 at 22:05
    
sed 's/[<|>|]/ /g' -- The s does the search; the / is the delimiter; the [] brackets let us search for more than one thing; the | vertical bar is a logical or and the g is to search globally or the whole file. –  Iman Feb 29 '12 at 22:11
3  
You don't need (or, indeed, want) the |s within []. use 's/[<>,]//g'. –  Kevin Feb 29 '12 at 22:19
2  
You don't need a vertical bar. Anything in the square brackets is searched for as a list. sed 's/[<>,]/ /g' will work exactly was well, except your idea will remove vertical bars as well. –  Paul Tomblin Feb 29 '12 at 22:19
    
To add: maybe you want to look into regular expressions, this is one of the easiest example, but already demonstrating the power. –  Bernhard Feb 29 '12 at 22:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.