0

I have string "[9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]"

in that I want to remove first "[" but unable to do this using below command,

sed 's/[//g'
1
5

Try doing this :

sed 's/\[//'

The [ ] are special metacharacters used to match a character from a set (like [a-z]).

4
  • On another note, you will be able to replace [ without the escape character :)
    – Citylight
    Nov 27 '14 at 18:00
  • No Sree: sed: -e expression #1, char 5: unterminated s' command` Nov 27 '14 at 18:21
  • @sputnick I think that error came from an unterminated s. I had asked a question here a few days back unix.stackexchange.com/questions/168240/…
    – Citylight
    Nov 28 '14 at 8:08
  • @Sree As the answer to your question explains, you can't use an unescaped [ Apr 29 '19 at 12:14
2
start cmd:> echo "[9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]" |
  sed 's/[[]//'
9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]
1

This would help you,

$ echo '[9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]'|sed 's/^\[//'
9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]
1

or directly with bash

code="[9920380315,POSTPAID,0009,646.26,SELF,APPLICATION]"
echo ${code/[/}
3
  • Unlike OP's sedcommand, this will only remove the first occurrence of [, use //
    – pLumo
    Apr 29 '19 at 12:18
  • yep but that was the request, remove the first [ of the string
    – darxmurf
    Apr 29 '19 at 12:21
  • sure, but it may be worth mentioning at least ;-)
    – pLumo
    Apr 29 '19 at 12:22
-1

or

$ echo "[HTML],[FLASK]"|sed 's/[][]//g'
HTML,FLASK
1
  • This removes more than the first [.
    – Kusalananda
    Jun 26 '19 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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