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' 
2
  • 1
    tr -d '[' (assuming a POSIX tr). Commented Nov 27, 2014 at 14:55
  • Where is this string stored? In a variable or in a text file? Commented Jan 27, 2022 at 21:21

6 Answers 6

6

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 :) Commented Nov 27, 2014 at 18:00
  • No Sree: sed: -e expression #1, char 5: unterminated s' command` Commented Nov 27, 2014 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/… Commented Nov 28, 2014 at 8:08
  • @Sree As the answer to your question explains, you can't use an unescaped [ Commented Apr 29, 2019 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 // Commented Apr 29, 2019 at 12:18
  • yep but that was the request, remove the first [ of the string Commented Apr 29, 2019 at 12:21
  • sure, but it may be worth mentioning at least ;-) Commented Apr 29, 2019 at 12:22
0

I got a single pass to work to remove both [ AND ] using a combined sed expression which I had not considered before with 2 expressions separated with a semicolon :

$ echo "ON bec.[BusinessID] = s.[BusinessID]" | sed 's/\[//g; s/\]//g' ON bec.BusinessID = s.BusinessID 
2
  • More efficiently done with tr -d '[]'. Also note that the request was for removing the first [ only. Commented Jan 27, 2022 at 21:23
  • 1
    This can be done more concisely (although less clearly) with sed 's/[][]//g', which was already given as an answer.  Also, FYI, you can do s/]// without escaping the ]. Commented Jan 27, 2022 at 23:49
-1

or

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.