开发者

Find pattern and edit text after the pattern using sed?

开发者 https://www.devze.com 2023-03-10 19:14 出处:网络
I have a file w开发者_Python百科hich consist of some text, my interest is this particular line :

I have a file w开发者_Python百科hich consist of some text, my interest is this particular line : BUILD = 0 where the value of BUILD keeps changing.

I need help in pattern searching for "BUILD = " and replace anything after this pattern with my new string.

For example: BUILD = test

And also I want this editing to happen on the original file and no redirection. How exactly can this be accomplished using sed ?

Thanks for the help in advance.


sed -i 's/\(BUILD = \)\(.*\)/\1hello/g' test.txt

note: -i as in "in place"


Here is some 'case-like' solution I came up with:

$ echo -e "0123\nBUILD = 2" | sed '/^BUILD =/ { s/0/a/; s/1/b/; s/2/c/; s/3/d/ }'
0123
BUILD = c

So the exact version of the command solving your problem is:

sed -i.bak '/^BUILD =/ { s/0/a/; s/1/b/; s/2/c/; s/3/d/ }' somefile.txt

The -i.bak option allows edit in place with backup to a file with .bak extension.

0

精彩评论

暂无评论...
验证码 换一张
取 消