简体   繁体   English

如何使用 sed 删除文件中的多行

[英]How to remove the multiple lines in the file using sed

The test.txt file contains 3 lines: test.txt文件包含 3 行:

STREET=main
PHONE=123
EMAIL=abc@xyz.com

To remove two lines with the STREET and EMAIL I run the sed twice in a row:要使用STREETEMAIL删除两条线,我连续两次运行sed

sed -i -- 's/STREET=.*//' test.txt
sed -i -- 's/EMAIL=.*//' test.txt

Instead of using the sed command twice I would rather remove both lines with a single sed command.而不是使用sed命令两次,我宁愿使用单个sed命令删除两行。 How to do it?怎么做?

To delete ( d ) lines which contain STREET= or EMAIL= .删除包含STREET=EMAIL=的 ( d ) 行。

sed -i -- '/STREET=/d; /EMAIL=/d' file
sed -i -- 's/STREET=.*//;s/EMAIL=.*//' test.txt

The following sed one-liners show how to remove/empty the target lines:以下 sed 单行显示如何删除/清空目标行:

Empty the target lines:清空目标行:

kent$  sed 's/^\(EMAIL\|STREET\)=.*//' file

PHONE=123

Remove the target lines:删除目标行:

kent$  sed '/^\(EMAIL\|STREET\)=/d' file
PHONE=123

Using pattern ^\(EMAIL\|STREET\)= will avoid to touch lines like USER_EMAIL=... or SOME_STREET=使用模式^\(EMAIL\|STREET\)=将避免触及像USER_EMAIL=...SOME_STREET=这样的行

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

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