搜索并替换两个不同字符串之间的内容

时间:2018-04-26 13:37:56

标签: regex sed

我的要求比现有答案here稍微复杂一些。

    \addplot3[%
surf, z buffer=sort, colormap={mymap}{[1pt] rgb(0pt)=(0.2081,0.1663,0.5292); rgb(1pt)=(0.211624,0.189781,0.577676); rgb(2pt)=(0.212252,0.213771,0.626971); rgb(3pt)=(0.2081,0.2386,0.677086); rgb(4pt)=(0.195905,0.264457,0.7279); rgb(5pt)=(0.170729,0.291938,0.779248); rgb(6pt)=(0.125271,0.324243,0.830271); rgb(7pt)=(0.0591333,0.359833,0.868333); rgb(8pt)=(0.0116952,0.38751,0.881957); rgb(9pt)=(0.00595714,0.408614,0.882843); rgb(10pt)=(0.0165143,0.4266,0.878633); rgb(11pt)=(0.0328524,0.443043,0.871957); rgb(12pt)=(0.0498143,0.458571,0.864057); rgb(13pt)=(0.0629333,0.47369,0.855438); rgb(14pt)=(0.0722667,0.488667,0.8467); rgb(15pt)=(0.0779429,0.503986,0.838371); rgb(16pt)=(0.0793476,0.520024,0.831181); rgb(17pt)=(0.0749429,0.537543,0.826271); rgb(18pt)=(0.0640571,0.556986,0.823957); rgb(19pt)=(0.0487714,0.577224,0.822829); rgb(20pt)=(0.0343429,0.596581,0.819852); rgb(21pt)=(0.0265,0.6137,0.8135); rgb(22pt)=(0.0238905,0.628662,0.803762); rgb(23pt)=(0.0230905,0.641786,0.791267); rgb(24pt)=(0.0227714,0.653486,0.776757); rgb(25pt)=(0.0266619,0.664195,0.760719); rgb(26pt)=(0.0383714,0.674271,0.743552); rgb(27pt)=(0.0589714,0.683757,0.725386); rgb(28pt)=(0.0843,0.692833,0.706167); rgb(29pt)=(0.113295,0.7015,0.685857); rgb(30pt)=(0.145271,0.709757,0.664629); rgb(31pt)=(0.180133,0.717657,0.642433); rgb(32pt)=(0.217829,0.725043,0.619262); rgb(33pt)=(0.258643,0.731714,0.595429); rgb(34pt)=(0.302171,0.737605,0.571186); rgb(35pt)=(0.348167,0.742433,0.547267); rgb(36pt)=(0.395257,0.7459,0.524443); rgb(37pt)=(0.44201,0.748081,0.503314); rgb(38pt)=(0.487124,0.749062,0.483976); rgb(39pt)=(0.530029,0.749114,0.466114); rgb(40pt)=(0.570857,0.748519,0.44939); rgb(41pt)=(0.609852,0.747314,0.433686); rgb(42pt)=(0.6473,0.7456,0.4188); rgb(43pt)=(0.683419,0.743476,0.404433); rgb(44pt)=(0.71841,0.741133,0.390476); rgb(45pt)=(0.752486,0.7384,0.376814); rgb(46pt)=(0.785843,0.735567,0.363271); rgb(47pt)=(0.818505,0.732733,0.34979); rgb(48pt)=(0.850657,0.7299,0.336029); rgb(49pt)=(0.882433,0.727433,0.3217); rgb(50pt)=(0.913933,0.725786,0.306276); rgb(51pt)=(0.944957,0.726114,0.288643); rgb(52pt)=(0.973895,0.731395,0.266648); rgb(53pt)=(0.993771,0.745457,0.240348); rgb(54pt)=(0.999043,0.765314,0.216414); rgb(55pt)=(0.995533,0.786057,0.196652); rgb(56pt)=(0.988,0.8066,0.179367); rgb(57pt)=(0.978857,0.827143,0.163314); rgb(58pt)=(0.9697,0.848138,0.147452); rgb(59pt)=(0.962586,0.870514,0.1309); rgb(60pt)=(0.958871,0.8949,0.113243); rgb(61pt)=(0.959824,0.921833,0.0948381); rgb(62pt)=(0.9661,0.951443,0.0755333); rgb(63pt)=(0.9763,0.9831,0.0538)}, mesh/rows=21]

以上是一段代码,它使用不同的rgb值重复多次。因此,简单的搜索和替换不起作用。

我想用以下内容替换上面的块:

\addplot3[%
surf, z buffer=sort, colormap={my}{rgb255=(20,20,20) rgb255=(50,50,50) rgb255=(250,250,250) }, mesh/rows=21]

每次更换的内容都相同。

如上所述,我在上面的回答链接中尝试\addplot3[WORD1},WORD2,我的要求surf, z buffer=sort, colormap={my}{rgb255=(20,20,20) rgb255=(50,50,50) rgb255=(250,250,250)WORD3

sed -i 's/(\addplot3[).*(},)/\1 surf, z buffer=sort, colormap={my}{ rgb255=(20,20,20) rgb255=(50,50,50) rgb255=(250,250,250) \2/g' multiparticle_random.tex

我收到以下错误:

sed: -e expression #1, char 126: unterminated `s' command

有人可以帮助您成功完成此搜索并替换操作吗?

欢迎适用于sed以外的Linux的解决方案。

1 个答案:

答案 0 :(得分:0)

首先应匹配以\\addplot3\[%开头的行(观察转义字符),然后将下一行读入模式空间(sed一次占用一行)并执行替换命令。要捕获使用sed(POSIX BRE)的群组,您应该将其转义\(...\)或使用-r选项:

sed '/\\addplot3\[%/{N;s/\(.*\)\n.*\(},.*\)/\1 surf, z buffer=sort, colormap={my}{ rgb255=(20,20,20) rgb255=(50,50,50) rgb255=(250,250,250) \2/g}' file