Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to delete single character after other character in line by sed

For example

   LINE="ABCDE 123 |@ TEST"

I want to delete the @ character only if it exist after the first "|" character in LINE

sed command will verify if: @ character exist after "|" , and if its true then sed will delete the @ character

other example

    LINE="qwe 123 >|@ sdf g"  (before sed action)

    LINE=="qwe 123 >| sdf g"  (after sed action)


share|improve this question
up vote 1 down vote accepted
 % echo "ABCDE 123 |@ TEST" | sed 's,|@,|,'

its essentially the same as your other question.

share|improve this answer
simple and work fine THX yael – yael Jun 28 '10 at 10:03

You seem to ask a lot of these questions. I suggest making yourself familiar with regular expression to understand the answers better and come up with your own solutions. is a good point to start.

To write a regular expression that matches lines containing your case (i.e. AT after PIPE), you'd use


Here ^ means the beginning of the line, .*? matches any characters, but as little as possible, @ is a literal @, and \| is a literal |, just escaped because it has special meaning in regular expressions.

To delete the AT sign, you instruct sed to replace the whole sentence with everything but the AT sign. To do this, you need to store everything you want to keep. Regular expressions manage this with capturing groups. You arrive at


The sed command you are looking for finally looks like

sed -r 's/^(.*?|.*?)@/\1/'

The command "s" stand for substitute, the regular expression we have created is between the first pair of / (with the brackets and backslash being escaped). The text to replace the match with is the \1 behind that and stands for the content of the first capturing group. Since we did not capture the AT, that should do the trick.

Hope to have helped. Since I have no sed here, this may or may not work without modifications. =)

share|improve this answer
too complicated, just search for '|@' and replace the first occurance. there is no need to backreference whats before or after '|@', sed will leave it alone anyway. – akira Jun 28 '10 at 9:58
Ah, thanks for the hint. =) I seldomly actually use sed. My solutions replaces "| bla @" with "| bla ", though. Dunno if that is what it should be doing. – Jens Jun 28 '10 at 10:01
Your final command needs to be either sed 's/^\(.*\?\|.*\?\)@/\1/' or sed -r 's/^(.*?|.*?)@/\1/' (corrections to the escaping) – Dennis Williamson Jun 28 '10 at 13:40
@Dennis: Thanks =) – Jens Jun 28 '10 at 14:04

Sorry, can't currently comment on Jens' solution.

The patterns discussed in Jens' solution do not meet the requirement to 'delete the @ character only ... after the first "|" character'. A conforming pattern would be:

sed 's/\(^[^|]|\)@/\1/'

This does not modify lines which have an additional PIPE before the PIPE AT sequence.

share|improve this answer

You must log in to answer this question.

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