Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

search - How to replace a text pattern containing brackets?

I want to replace a specific instruction containing brackets with another instruction recursively in all the files.

For example,

mov r1, [r1, r2]

with

sub [r8, r9], r10

When I use

 sed -i.bak "s/mov r1, [r1, r2]/sub [r8, r9], r10/g" file.S

it doesn't work.

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two things.

  1. You need to escape the brackets in the match expression, they have special meaning in regular expressions.
  2. You should protect the sed script from shell expansion. Using double quotes forces expansions. Single quotes switches expansion off.

Thus:

's/mov r1, [r1, r2]/sub [r8, r9], r10/g'

While working out the correct script you can just skip the inline editing, maybe. Like so:

sed 's/mov r1, [r1, r2]/sub [r8, r9], r10/g' file.S

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...