Delete character in a certain position?


How do I delete a particular character in a particular position in the shell?

I've tried with sed , but I can not put the position too, just the default.


What I wanted was to delete the , that is in the second position of the string, but if there is no , nothing should be done.

asked by anonymous 30.04.2014 / 21:24

2 answers


You can do this:

~$ echo ",45123","B23142DHAS675" | sed 's/.//7'

Where 7 is the position of the character to be removed. Another alternative:

~$ echo ",45123","B23142DHAS675" | sed 's/\,//2'

The above command removes the second occurrence of , .

01.05.2014 / 01:55

Use a regular expression that stores the first% w / o% characters, make sure that the n - 1 character is what you want, and save the remaining characters.

 echo "abcdef nao sera removido
a,cdef será removido
A,BCD será removido" | sed -r 's/^(.),(.*)$//'


abcdef nao sera removido
acdef será removido
ABCD será removido
30.04.2014 / 23:30