复杂的sed命令语法

时间:2018-02-08 10:01:00

标签: linux ubuntu sed

我有这个* .sql文件:

...some random text here ',0,'here/there/is/always/this/path',here some other random text...
...some random text here ',1,'here/there/is/always/this/path',here some other random text...
...some random text here ',2,'here/there/is/always/this/path',here some other random text...
...some random text here ',3,'here/there/is/always/this/path',here some other random text...
...some random text here ',4,'here/there/is/always/this/path',here some other random text...
[...]
...some random text here ',n,'here/there/is/always/this/path',here some other random text...

我需要在ID为>的路径的开头添加此路径的新部分。 0

预期结果:

...some random text here ',0,'here/there/is/always/this/path',here some other random text...
...some random text here ',1,'newpart/here/there/is/always/this/path',here some other random text...
...some random text here ',2,'newpart/here/there/is/always/this/path',here some other random text...
...some random text here ',3,'newpart/here/there/is/always/this/path',here some other random text...
...some random text here ',4,'newpart/here/there/is/always/this/path',here some other random text...
[...]
...some random text here ',n,'newpart/here/there/is/always/this/path',here some other random text...

可以使用sed命令还是太复杂了?我在Ubuntu 16.04

编辑:如果你在没有告诉我我做错了什么的情况下向我提出问题,那么你将无所作为,下次我将永远不会学到更好的东西。 感谢。

2 个答案:

答案 0 :(得分:1)

假设随机文本不会匹配,0,'here/there/is/always/this/path',,那么它是可行的:

sed "s%\\(,[1-9][0-9]*,'\\)\\(here/there/is/always/this/path',\\)%\\1newpart/\\2%" …

数据中的单引号表示在正则表达式周围使用双引号(可能)更好,但这意味着你必须加倍反斜杠。啊!我更喜欢将命令放入文件(script.sed):

s%\(,[1-9][0-9]*,'\)\(here/there/is/always/this/path',\)%\1newpart/\2%

并运行:

sed -f script.sed …

经过轻微测试的代码 - 它不会将行转换为',n,',因为它不是数字。

答案 1 :(得分:0)

如果数字字段前没有逗号,可能是

'newpart/

条件检查第二个字段是否大于零,并且操作表示在第三个字段之前插入1,然后在起始单引号后插回前一个值的子字符串。 (在单引号内,我们不能逐字插入文字单引号,因为这会终止引用的字符串;幸运的是,Awk允许我们为同一个字符使用八进制转义。)最终declare @myTable table( FilterName varchar(100) , FilterOption varchar(100)) insert into @myTable values ('Naam Adverteerder', 'kras'),('Branche', 'toerisme en vrije tijd'),('Mediumtype', 'krant'),('Formaat', 'kwart page (1/4)'),('Positie', 'linker (pagina)'),('Soort Uiting', 'print'),('Type', 'post-test'),('Ad', 'ja'),('Ad', 'nee'),('De Volkskrant', 'nee'),('De Volkskrant', 'ja'),('Trouw', 'nee'),('Trouw', 'ja'),('Het Parool', 'nee'),('Het Parool', 'ja'),('Bn Destem', 'nee'),('Bn Destem', 'ja'),('Brabants Dagblad', 'nee'),('Brabants Dagblad', 'ja'),('De Gelderlander', 'nee'),('De Gelderlander', 'ja'),('De Stentor', 'nee'),('De Stentor', 'ja'),('Tubantia', 'nee'),('Tubantia', 'ja'),('Eindhovens Dagblad', 'nee'),('Eindhovens Dagblad', 'ja'),('Pzc', 'nee'),('Pzc', 'ja'),('Magazine', 'nee'),('Magazine', 'ja'),('Hah', 'nee'),('Hah', 'ja'),('Ad nl', 'nee'),('Ad nl', 'ja'),('Vk nl', 'nee'),('Vk nl', 'ja'),('Tr nl', 'nee'),('Tr nl', 'ja'),('Parool nl', 'nee'),('Parool nl', 'ja'),('Regionale Website', 'nee'),('Regionale Website', 'ja'),('Week', '41'),('Kwartaal', '4'),('Jaar', '2016'),('Sample', 'breed'),('Naam Adverteerder', 'bol.com') select FilterName, FilterOption from @myTable 对无条件地打印每一行。