Bash - 在文件中查找关键字并删除其行

发布于 2024-11-29 07:27:57 字数 1034 浏览 1 评论 0原文

我想给出一个关键字,找到该关键字在文件中出现的行并删除整行。

这是我得到的,但它没有按预期工作:

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' ~/Desktop/hosts 但它不起作用。

结论

这是我想出的代码(基于好人给我的帮助):

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖公主 2024-12-06 07:27:57

使用流编辑器,sed:

sed -i ".bak" '/culpa/d' test.txt

以上将删除 test.txt 中包含 culpa 的行。它将创建原始文件的备份(名为 test.txt.bak)并就地修改原始文件。

Use the stream editor, sed:

sed -i ".bak" '/culpa/d' test.txt

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.

遗忘曾经 2024-12-06 07:27:57

将其通过管道传输到另一个文件,而不是您正在读取的文件,并小心 cat 的无用使用。

grep -v "$KEYWORD" /etc/hosts > newfile

除了关于 sed 给出的很好的答案之外,您还可以使用 Perl 和稍微改进的正则表达式来解决这个问题:

perl -pi.old -e 's/.*\sdomain\.com\s*\n//' file

注意我正在考虑 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.

grep -v "$KEYWORD" /etc/hosts > newfile

Apart from the fine answer given regarding sed, you can also use Perl and a little improved regex to solve this:

perl -pi.old -e 's/.*\sdomain\.com\s*\n//' file

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.

不如归去 2024-12-06 07:27:57

如果您只想删除示例文件的最后一行

sed -i '/[[:space:]]\+domain\.com/d' test.txt

If you want to only delete last line of your example file

sed -i '/[[:space:]]\+domain\.com/d' test.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文