使用sed / grep更新匹配行

时间:2012-11-23 13:45:53

标签: linux sed grep

我需要用另一个字符串替换模式匹配线。

实施例

line1
line2
the serial port is ttyS0
line4

我需要将包含ttyS0的行替换为

line1
line2
You have to use usb interface
line4

我怎样才能在sed(或grep)

中做到这一点

我试过了sed 's/ttyS0/You have to use usb interface/' myfile。但它只替换了该行中匹配的单词。我想替换整行。

2 个答案:

答案 0 :(得分:3)

像这样:

sed 's/.*ttyS0.*/You have to use usb interface/' myfile

答案 1 :(得分:3)

使用change选项的一种方式:

sed '/ttyS0/c\You have to use usb interface' myfile