WraithLux
WraithLux

Reputation: 699

Replace numbers one by one with regex

I have strings such as this:

"Query_string" : [ 1345.6423, 5656.5, 346.324, 880.0 ],
"Query_string" : [ 1345.6423, 5656.5, 346.324, 880.0 ],
"Query_string" : [ 1345.6423, 5656.5, 346.324, 880.0 ],
Random code 124253
String.....

I need to replace digits that have "query_string" in front of them to be zero, like so:

"Query_string" : [ 0000.0000, 0000.0, 000.000, 000.0 ],

But other stuff should stay in place, eg:

Random code 124253

I tried this:

(^\"Query\_string\"\s\:\s\[\s)|\d|(\s\]\,)

But it matches all digits, including "Random code 124253"

Upvotes: 0

Views: 54

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed ": loop
s/\("Query_string".*\)[1-9]/\10/
t loop" YourFile

Upvotes: 1

perreal
perreal

Reputation: 97938

This sed expression replaces all non-zero digits on Query lines with 0:

 sed '/^"Query_string"/{s/[1-9]/0/g}' input

Another version:

sed '/^"Query_string"/!b;s/[1-9]/0/g' input

Still another:

sed '/^"Query_string"/s/[1-9]/0/g' input

Upvotes: 0

Related Questions