Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The following sed syntax deletes the first @ character if it exists in a string:

 sed 's/^@\(.*\)/\1/'

But how to change it to delete the @ character only if it's in the third position? For example, I would need to delete it in the following line:


I need the script to verify if the third character is @, and if so then to delete it from the line.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You can use

sed 's/^\(..\)@\(.*\)/\1\2/'
share|improve this answer
Very good many THX yael –  yael Jun 28 '10 at 7:13
If it works, consider accepting this as an answer. =) –  Jens Jun 28 '10 at 7:16
you do not need the rest of the line, s/^\(..\)@/\1' is just fine. –  akira Jun 28 '10 at 7:22

To generalize for any character at any given position n:

sed 's/^\(.\{2\}\)./\1/'

which deletes the third character. Just change the number "2" to n-1.

You can use a variable like this:

echo "$line" | sed "s/^\(.\{${n}\}\)./\1/"
share|improve this answer
% sed -e '/^..@/ { s,^\(..\)@,\1, }'


  • if line starts with 3 characters, the third is a '@'
  • throw out the @, but keep the first chars
share|improve this answer

Your Answer


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.