hits_lucky
hits_lucky

Reputation: 337

Find pattern and edit text after the pattern using sed?

I have a file which 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.

Upvotes: 1

Views: 1924

Answers (2)

Rajish
Rajish

Reputation: 6805

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.

Upvotes: 1

bpgergo
bpgergo

Reputation: 16037

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

note: -i as in "in place"

Upvotes: 3

Related Questions