the following sed syntax delete the first @ character if exist in string

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

How to change the follwoing sed syntax in order to delete the third character? For example :

In the following line I need to delete the third character @

 line=AB@CDEF

sed syntax need to verify if the third character is: @ and if it true then to delete the @ from the line

Yael

link|improve this question

36% accept rate
feedback

3 Answers

up vote 1 down vote accepted

You can use

sed 's/^\(..\)@\(.*\)/\1\2/'
link|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
feedback

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:

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

means:

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

Your Answer

 
or
required, but never shown

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