Kraang Prime
Kraang Prime

Reputation: 10479

How can I replace this string inside a file using bash?

Would like to write a bash script that replaces some strings in a text file, however I am getting hung up on how to replace the contents inside a 'value' for a variable.

Suppose the value I wish to set inside a text file could be :

TCP_IN = "123,35,995"

.. where 123,35,995 could be any string (not just numbers with commas.

How could I replace this keypair searching for TCP_IN = "*" and set the value inside where * is, from within a bash script ?

Upvotes: 1

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You can use

sed -E 's/(TCP_IN *= *")[^"]+/\1MyNewVal/g'

See the IDEONE demo

The regex matches and captures into Group 1 TCP_IN followed with zero or more spaces, followed with a = symbol, followed with again zero or more spaces, and then [^"]+ matches 1 or more characters other than a " ([^...] is a *negated character class that matches all characters but those defined inside the char class).

Upvotes: 1

Related Questions