How do i replace [] brackets using SED
Asked Answered
S

6

19

I have a string that i am want to remove punctuation from.

I started with

sed 's/[[:punct:]]/ /g'

But i had problems on HP-UX not liking that all the time, and some times i would get a 0 and anything after a $ in my string would dissappear. So i decided to try to do it manually.

I have the following code which works on all my punctuation that I am interested in, except I cannot seem to add square brackets "[]" to my sed with anything else, otherwise it does not replace anything, and i dont get an error, so I am not sure what to fix.

Anyways this is what i currently have and would like to add [] to.

sed 's/[-=+|~!@#\$%^&*(){}:;'\'''\"''\`''\.''\/''\\']/ /g'

BTW I am using KSH on Solaris, Redhat & HP

Sisneros answered 30/8, 2012 at 19:20 Comment(1)
Why are you using sed for this? To replace a large group of characters with a space, it is better to use tr.Cnut
S
1

Here is the final code I ended up with

`echo "$string" | sed 's/[^a-zA-Z0-9]/ /g'`

I had to put = and - at the very end.

Sisneros answered 4/9, 2012 at 15:14 Comment(0)
C
29

You need to place the brackets early in the expression:

sed 's/[][=+...-]/ /g'

By placing the ']' as the first character immediately after the opening bracket, it is interpreted as a member of the character set rather than a closing bracket. Placing a '[' anywhere inside the brackets makes it a member of the set.

For this particular character set, you also need to deal with - specially, since you are not trying to build a range of characters between [ and =. So put the - at the end of the class.

Cnut answered 30/8, 2012 at 19:23 Comment(5)
Yea..that is what i was originally thinking, but when i did that I always recieved an invalid range end error echo word1$word2 | sed 's/[]-=+|~!@#\$%^&*(){}:;'\'''\"''`''\.''\/''\\'[]/ /g' sed: -e expression #1, char 36: Invalid range endSisneros
The 'invalid range' is because of the -. When - appears in the middle, sed is trying to create a range of characters. Use \- instead.Cnut
The - must be the in first character position in the character class to represent a dash otherwise it may be interpreted as a character class metacharacter that indicates a range of characters.Pettit
@potong, - can also be the last character of the set.Tyler
thanks for the tips...I posted my final result as an answer below...I had to move the = as well to the end of the regex for it to work.Sisneros
T
5

You can also specify the characters you want to keep [with inversion]:

sed 's/[^a-zA-Z0-9]/ /g'
Teratogenic answered 30/8, 2012 at 19:26 Comment(3)
Thats something I did not even realize was possible. Seems to work pretty good, except for when there is a "$" in my string...anything after the "$" is removed. I tried echo word1$word2 | sed 's/[^a-zA-Z0-9]/ /g' and my output was word1Sisneros
it is the shell interpreting $word2 as a variable, try echo 'word1$word2' or echo word1\$word2Teratogenic
What I ended up doing eventually was saving my string as a variable...then passing the variable inside double quotes. echo "$string" | sed 's/[^a-zA-Z0-9]/ /g'Sisneros
T
3

You can do it manually:

sed 's/[][\/$*.^|@#{}~&()_:;%+"='\'',`><?!-]/ /g'

This remove the 32 punctuation character, the order of some characters is important:

  • - should be at the end like this -]
  • [] should be like that [][other characters]
  • ' should be escaped like that '\''
  • not begin with ^ like in [^
  • not begin with [. [= [: and end with .] =] :]
  • not end with $]

here you can have explication of why all that http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_03

Terefah answered 21/10, 2016 at 1:44 Comment(0)
S
1

Here is the final code I ended up with

`echo "$string" | sed 's/[^a-zA-Z0-9]/ /g'`

I had to put = and - at the very end.

Sisneros answered 4/9, 2012 at 15:14 Comment(0)
S
1

If you need to replace the brackets totally along with the content then you need to escape it. For example below, replacing the whole brackets along with colon

echo "listen [::]:8080 default_server" sed -i 's|listen \[::\]:8080 default_server|listen       8080|' filename.txt
Stome answered 20/4, 2022 at 9:58 Comment(0)
O
0

Can be handled using the regex capture technique too (Eg: here below) :

echo "narrowPeak_SP1[FLAG]" | sed -e 's/\[\([a-zA-Z0-9]*\)\]/_\1/g'
> narrowPeak_SP1_FLAG

\[ : literal match to open square bracket, since [] is a valid regex
\] : literal match to square close bracket
\(...\) : capture group
\1 : represents the capture group within the square brackets
Outlet answered 25/4, 2017 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.