0

I am writing a bash script to replace a pattern in a line containing white space. Text looks like this - property name="abcd" value="1234" and the value changes multiple times in the file like value=3456 and value=3689. Only the line containing name="abcd" and value=something something should be replaced.

There are other instance where abcd and value are called separately which should NOT be replaced.

I want to match both criteria name="abcd" and value before doing replacement. Something like

sed -i '/name="abcd" value=/c\property name="abcd" value="set"' filename

How do I resolve that whitespace between "abcd" and value so that sed can recognize it as a single string?

Input:

`property name="abcd" value="1234"`
`property name="abcd" value="73845"`
`property name="abcd" value="8276"`
`property name="qwerty" value="1234"`
`blah blah ${abcd} blah`

Desired output:

`property name="abcd" value="set"`
`property name="abcd" value="set"`
`property name="abcd" value="set"`
`property name="qwerty" value="1234"`
`blah blah ${abcd} blah`
Rahul
  • 1
  • 2

3 Answers3

0

If I understand correctly, you have name="blah" value="blah" on seperate lines, and want to just remove the \n

    sed ':a;N;$!ba;s/\n/ /g' 

would do what you want taken from How can I replace a newline (\n) using sed?

Community
  • 1
  • 1
Rummy
  • 105
  • 5
0

After a few trial and errors, I hit upon the solution

`sed -i "/V.prop.*value=/c\ <property name=\\\"V.prop\\\" value=\\\"set\\\"/>" filename` 

The .* fills in between the two search patterns in the same line. The \\ is used for escaping double quotes.

Rahul
  • 1
  • 2
0

Try this:

sed -i -r 's/(property name="abcd" value=)"[0-9]+"/\1"set"/' filename
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328