Issue
So I have this file
a chair x 0 y
a table x 0 y
a window x 0 y
a computer x 0 y
I want to replace the 0
value with 111
value for the rows that have window
in them. So the output will look like:
a chair x 0 y
a table x 0 y
a window x 111 y
a computer x 0 y
Running sed 's/window.*0/y/' a.txt
does not give me what I want.
Solution
sed "s/ window \(.\) 0 / window \1 111 /"
Parenthesis (which need to be escaped) "group" a match. You can refer to groups by sequential number in your replacement string, preserving the matched value.
As it is unclear what your "a" / "x"/ "y" could stand in for (are they always "a"/"x"/"y", or always a single lowercase letter, or...), I can't really give you an "optimal" sed
line. If this is to be release-quality code, you need to make sure there can be no "false positives" (i.e. if "window" can appear at some other place than the second column). You could use the grouping mechanics I showed above to match the whole line, ensuring that "window" is in the second column... which could get hairy if a column could conceivably contain spaces. As I said, not enough information.
Answered By - DevSolar Answer Checked By - Clifford M. (WPSolving Volunteer)