简体   繁体   English

用sed / grep更新匹配行

[英]Updating matching line with sed/grep

I need to replace pattern matching line with yet another string. 我需要用另一个字符串替换模式匹配行。

Example

line1
line2
the serial port is ttyS0
line4

I need to replace line containing ttyS0 as 我需要将包含ttyS0行替换为

line1
line2
You have to use usb interface
line4

How can I do that in sed (or grep) 我该如何在sed(或grep)中做到这一点

I tried sed 's/ttyS0/You have to use usb interface/' myfile . 我试过sed 's/ttyS0/You have to use usb interface/' myfile But it replaces only the matching words from the line. 但是它仅替换行中匹配的单词。 I want to replace the complete line. 我要替换整行。

像这样:

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

一种使用更改选项的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM