Bash - 在文件中查找关键字并删除其行
我想给出一个关键字,找到该关键字在文件中出现的行并删除整行。
这是我得到的,但它没有按预期工作:
KEYWORD='domain.com'
cat /etc/hosts | grep -v "$KEYWORD" > /etc/hosts
更新
这对我有用:
sed -i_bak -e '/domain\.com/d' /etc/hosts
但是,由于我有两行包含“domain.com”,有没有办法告诉 sed 仅删除出现确切关键字“domain.com”的行
这是 /etc/hosts 的原始内容:
127.0.0.1 localhost localhost.localdomain
222.111.22.222 hvn.domain.com
222.111.22.222 domain.com
结局是这样的在命令 sed -i_bak -e '/domain\.com/d' /etc/hosts 之后:
127.0.0.1 localhost localhost.localdomain
我尝试了 sed -i_bak -e '/\
结论
这是我想出的代码(基于好人给我的帮助):
D=domain.com
DOMAIN=`echo "$D" | sed 's/\./\\\\./g'`
sed -i_bak -e "/[\t]$DOMAIN/d" /etc/hosts
请注意:
我计算在要删除的域之前总是有一个选项卡
我自动转义了域名的点。
I'd like to give a keyword, find the line where this keyword aṕpears in a file and erase the entire line.
This is what I got but it is not working as it should:
KEYWORD='domain.com'
cat /etc/hosts | grep -v "$KEYWORD" > /etc/hosts
UPDATE
This is what worked for me:
sed -i_bak -e '/domain\.com/d' /etc/hosts
However, as I had two lines with "domain.com", is there a way to tell sed to erase only the line where the exact keyword "domain.com" appears
This is the original content of /etc/hosts:
127.0.0.1 localhost localhost.localdomain
222.111.22.222 hvn.domain.com
222.111.22.222 domain.com
Here's how it end up after the command sed -i_bak -e '/domain\.com/d' /etc/hosts
:
127.0.0.1 localhost localhost.localdomain
I tried sed -i_bak -e '/\<namourphoto\.com\.br\>/d' ~/Desktop/hosts
but it didn't work.
CONCLUSION
This is the code I came up with (based on the help the fine folks have given me):
D=domain.com
DOMAIN=`echo "$D" | sed 's/\./\\\\./g'`
sed -i_bak -e "/[\t]$DOMAIN/d" /etc/hosts
Note that:
I am counting that there is always a tab before the domain to be erased
I am automatically escaping the dots of the domain name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用流编辑器,sed:
以上将删除 test.txt 中包含 culpa 的行。它将创建原始文件的备份(名为 test.txt.bak)并就地修改原始文件。
Use the stream editor, sed:
The above will delete lines containing culpa in test.txt. It will create a backup of the original (named test.txt.bak) and will modify the original file in-place.
将其通过管道传输到另一个文件,而不是您正在读取的文件,并小心
cat
的无用使用。除了关于 sed 给出的很好的答案之外,您还可以使用 Perl 和稍微改进的正则表达式来解决这个问题:
注意我正在考虑
domain.com
将由空格字符(制表符或空格,以及等等),并且在它后面只会出现零个或多个空格,直到换行为止。与 sed 中的-i
类似,perl 中的
-i.old
设置$^I
变量,您的原始文件将收到更改,而副本将保留在旧名称和下.old 附加
。Pipe it to another file, not the same one that you're reading from, and be careful with the useless use of
cat
.Apart from the fine answer given regarding sed, you can also use Perl and a little improved regex to solve this:
Notice I'm considering
domain.com
will be isolated by space characters (tabs or spaces, and so on), and that nothing but zero or more spaces will appear after it until the newline. Similarly to-i
in sed,-i.old
in perl sets the$^I
variable, and your original file will receive the changes while a copy will be kept under the old name and a.old
appended.如果您只想删除示例文件的最后一行
If you want to only delete last line of your example file