user11509937
user11509937

Reputation:

Replace float like number in line

I would like to increase the version number in a config file in both lines which looks like this:

version:
  abc01: 1.13.0
  abc02: 1.13.0

To do that I have tried to use sed command - match abc01 and abc02 and then replace them with abc0X 1.14.0 but I think it is not the ideal solution and it was missing TABs.

sed -i -e 's/.*abc01.*$/\abc01 1.14.0/g' config.yaml

Upvotes: 1

Views: 201

Answers (3)

Cyrus
Cyrus

Reputation: 88583

With awk:

awk -v FS='.' -v OFS='.' '/abc/{$2++} {print}' file

or

awk -v FS='.' -v OFS='.' '/abc/{$2++}1' file

or

awk 'BEGIN{FS=OFS="."} /abc/{$2++}1' file

or

awk '/abc/{$2++}1' FS='.' OFS='.'  file

Output:

version:
  abc01: 1.14.0
  abc02: 1.14.0

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Upvotes: 1

SLePort
SLePort

Reputation: 15461

Try this:

sed -E 's/(abc0.: 1\.1)./\14/' config.yaml

Upvotes: 0

Aaron
Aaron

Reputation: 24802

If the abc01 doesn't appear anywhere else in your input, the easiest way to solve this is to avoid matching the whitespaces in front of it so that you will leave it untouched :

sed -i -e 's/abc01.*$/abc01 1.14.0/g' config.yaml

If that isn't the case, then you can capture the start of the line in a capturing group you'll reference in your replacement pattern :

sed -i -E 's/(.*abc01).*$/\1 1.14.0/g' config.yaml

Upvotes: 1

Related Questions