【发布时间】:2014-10-04 09:41:02
【问题描述】:
我无法使用 sed 进行简单的正则表达式替换。这是我遇到的例子。
我正在尝试替换文件中的行,来自:
select * from mytable where mytable.s_id=274
select * from mytable where mytable.s_id=275
select * from mytable where mytable.s_id=276
select * from othertable where othertable.id=?
select * from table3 where table3.name=?
到这里:
select * from mytable where mytable.s_id=?
select * from mytable where mytable.s_id=?
select * from mytable where mytable.s_id=?
select * from othertable where othertable.id=?
select * from table3 where table3.name=?
我现在正在使用 sed:
cat log | sed 's/where mytable\.s_id=[0-9]+/where mytable.s_id=/g' | sort
但是我在 sed ('s/where mytable\.s_id=[0-9]+/where mytable.s_id=/g') 中使用的正则表达式并不能替代任何东西。
我正在阅读尽可能多的文档,但我阅读的所有内容都不同,做事方式也不同,所以我有点迷茫。使用 sed 进行正则表达式替换的规范方法是什么,在我的特殊情况下会是什么样子?
注意:我简化了我面临的问题。我确实想使用 sed(最终学会使用它)并且我 确实想通过管道传输输入和输出(而不是就地编辑文件),因为命令行我用的其实比那个复杂。
【问题讨论】:
-
.s和id之间有空格还是下划线?
-
您似乎在使用支持
+的GNU sed。您要么需要通过添加\来转义它,要么使用扩展正则表达式开关-r运行sed。