Reputation: 13
Using sed, trying to achieve something like below.
Existing text: Value 7
New text: Value 2
The trick here is, I am trying to replace all the files in a directory with multiple values (Ex: Value 7, Value 6, Value 27, Value 108,...) with Value 2.
Here's what I tried already:
sed -i 's/Value */Value 2/g' *
But this does replace only the Value (Ex: It replaces Value 6 to Value 26).It replaces "Value" with "Value 2"
Tried So far:
sed -i 's/Value */Value 2/g' *
sed -i 's/Value '*'/Value 2/g' *
It still replaces Value only but not the number. I need all the "Value *" to be replaced with "Value 2"
Upvotes: 0
Views: 74
Reputation: 58568
This might work for you (GNU sed):
sed -E 's/\<(Value [^,]+, )*(Value) [^,]*/\2 2/g' file
This replaces a list of values or a single value with a single value, globally within a line.
Upvotes: 0
Reputation: 50805
Value *
matches Value
followed by zero or more spaces. You should use Value [0-9]*
instead. Like:
sed -i 's/Value [0-9]*/Value 2/g' *
or you can put Value
in a capturing group to avoid rewriting it:
sed -i 's/\(Value \)[0-9]*/\12/g' *
Upvotes: 1