LoginSignup
1
0

More than 3 years have passed since last update.

角括弧[]を正規表現で置換

Last updated at Posted at 2020-02-12

角括弧を置換したい


[abc]


"abc"

にしたい。

エスケープすればいいよね?


$ echo [abc] | sed 's/[\[\]]/\"/g' 
[abc]

おや。

正解

こちらによると、


$ echo [abc] | sed 's/[][]/\"/g' 
"abc"

できた。

理由

はこちら
https://www.gnu.org/software/sed/manual/sed.html#Character-Classes-and-Bracket-Expressions
「sed, a stream editor」

「5.5 Character Classes and Bracket Expressions」
によると、

‘]’
ends the bracket expression if it’s not the first list item. So, if you want to make the ‘]’ character a list item, you must put it first.

ということで、] は最初に配置する必要があるらしい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0