2

I have *.dot file, for example:

...
0 -> 1 [color=black];
1 -> 2 [color=blue];
1 -> 3 [color=blue];
2 -> 4 [color=gold3];
..

I need to change "color" of lines whose started with $a number. I can easy get =blue using

a="1"
cat experimental.dot | grep "^$a\ ->" | grep -o =[a-Z0-9]*

But i can't change =blue to =red in file using sed.

4

2 回答 2

3
a=1
sed "/^$a /s/\\(color=\\)[[:alnum:]]\\+/\\1red/" <<END
0 -> 1 [color=black];
1 -> 2 [color=blue];
1 -> 3 [color=blue];
2 -> 4 [color=gold3];
END
0 -> 1 [color=black];
1 -> 2 [color=red];
1 -> 3 [color=red];
2 -> 4 [color=gold3];

For all lines beginning with your variable value (adding the space so you don't match "10" or "11") change the word following "color=" to red.

于 2013-09-15T23:09:24.493 回答
2

It probably should be like this:

a="1"
sed "/^$a ->/s/=blue/=red/" experimental.dot

Output:

0 -> 1 [color=black];
1 -> 2 [color=red];
1 -> 3 [color=red];
2 -> 4 [color=gold3];

If you want to modify the file, use -i:

sed -i "/^$a ->/s/=blue/=red/" experimental.dot
于 2013-09-15T23:12:12.777 回答