39

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 didn't 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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

77

With sed:

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

With tr:

tr -d '<>,'
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • 4
    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 01 '12 at 00: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 02 '12 at 14:40
  • What language/syntax uses pipes instead of slashes? – ram4nd Mar 05 '20 at 08:55
13

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

Iman
  • 159
  • 4
  • What are the vertical bars there for? – Paul Tomblin Feb 29 '12 at 22:05
  • 1
    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
  • 1
    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
  • you are right I just looked back; I get my programming and scripting mixed up. – Iman Feb 29 '12 at 22:54
  • @Iman: Please consider editing your answer. – Keith Thompson Feb 29 '12 at 23:21
  • I thought I did let me try again Thanks @Keith – Iman Mar 01 '12 at 00:21
  • This will also remove spaces, which is not desired. – Chris Down Mar 01 '12 at 01:20
  • @Iman: You are nearly there, but as your command appears at the moment, it will replace each [<>,] with a space. To delete the characters, your need to remove the space from between the last two delimiters. – Peter.O Mar 01 '12 at 05:36