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?

5
  • 2
    bash-3.2$ echo "<230948203[234]>," | tr '<>,' ' ' --> 230948203[234] -- EDITED Thanks to Paul
    – user14039
    Feb 29, 2012 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). Feb 29, 2012 at 23:23
  • 1
    @KeithThompson: tr -d '<>,', without '' in the end, not? Mar 1, 2012 at 22:46
  • @userunknown: Yes, thanks for the correction. Mar 1, 2012 at 22:47
  • For my particular application, sed worked where tr was throwing errors. That is likely because I was working inside a Github Action and maybe because I do not fully understand tr!
    – cptully
    May 30, 2023 at 15:08

2 Answers 2

77

With sed:

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

With tr:

tr -d '<>,'
5
  • 4
    Correct, but I personally find the use of | as a delimiter a bit confusing. sed 's/[<>]//g' is a bit easier to read. Feb 29, 2012 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, 2012 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. Mar 1, 2012 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, 2012 at 14:40
  • What language/syntax uses pipes instead of slashes?
    – ram4nd
    Mar 5, 2020 at 8:55
13

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

10
  • What are the vertical bars there for? Feb 29, 2012 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, 2012 at 22:11
  • 3
    You don't need (or, indeed, want) the |s within []. use 's/[<>,]//g'.
    – Kevin
    Feb 29, 2012 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. Feb 29, 2012 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, 2012 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .