简体   繁体   English

sed删除所有包含密码一词的行。但是不要保留空行

[英]sed remove all lines containing the word password.But don't keep the empty line

My file has lines 我的文件有行

Database Name:Mydb
DatabaseServer:DbServer
Password:Example
Username:User1

Database Name:Mydb1
DatabaseServer:DbServer1
Password:Example1
Username:User11

I used sed -i "s/password. //gI" file but that is leaving an empty line(in place of password. ) which i don't want. 我使用了sed -i“ s / password。// gI”文件,但这留下了我不希望的空行(代替密码。 )。

Desired result: 所需结果:

Database Name:Mydb
DatabaseServer:DbServer
Username:User1

Database Name:Mydb1
DatabaseServer:DbServer1
Username:User11

试试看:

sed -i '/Password:/d' file

Solution 1st: grep here to help. 解决方案1: grep在这里提供帮助。

grep -v '^Password'  Input_file

In case you need to save into same Input_file then you could following. 如果您需要保存到相同的Input_file中,则可以执行以下操作。

grep -v '^Password'  Input_file > temp && mv temp Input_file

Solution 2nd: Using awk . 解决方案2nd:使用awk

awk '!/^Password/' Input_file > temp_file && mv temp_file Input_file

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

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