Amit Kumar Mondal
Amit Kumar Mondal

Reputation: 57

Replace String from all files

I am trying to find all CONFIG.INF files and change a specific property to a specific one.

The CONFIG.INF file looks as follows:

NAME: A
VERSION: 1.2.0.X
TOPIC: X

I want to read such files and replace the last part from the version property of each file.

find ${WORKSPACE} -name CONFIG.INF -type f -exec sed -i.bak "s/VERSION:.*/VERSION: ${VERSION}/g" {} \;

This replaces the complete version but I want to replace only the last part from the version. According to the example, the version will be 1.2.0.Y if I replace with Y. The last part could be anything (not necessarily X) and I just want to replace the last part.

Upvotes: 1

Views: 52

Answers (1)

glenn jackman
glenn jackman

Reputation: 246744

You could do

sed -i.bak "s/VERSION:.*\./&${VERSION_LETTER}/g"

That matches the string "VERSION:" followed by as many characters as possible until the last dot.

The replacement part contains the magic character & which is replaced by the text that was matched by the regular expression, followed by the variable that contains the new version letter.

Upvotes: 2

Related Questions