How do i replace [] brackets using SED

微笑、不失礼 提交于 2019-12-18 18:57:09

问题


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


回答1:


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.




回答2:


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

sed 's/[^a-zA-Z0-9]/ /g'



回答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




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/12203963/how-do-i-replace-brackets-using-sed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!