问题描述
我想给一个关键字,找到行其中,在该文件中出现的关键字和擦除整条生产线。
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
更新
这是对我工作:
的sed -e -i_bak/domain\\.com/d/ etc / hosts中
不过,我有两条线domain.com,有没有办法告诉SED,其确切的关键字domain.com似乎只删除行
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
这是/ etc / hosts文件的原始内容:
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
下面的命令后是如何结束的sed -e -i_bak/domain\\.com/d/ etc / hosts中
:
127.0.0.1 localhost localhost.localdomain
我试过的sed -e -i_bak/\\<namourphoto\\.com\\.br\\>/d〜/桌面/主机
,但它没'将不起作用。
I tried sed -i_bak -e '/\<namourphoto\.com\.br\>/d' ~/Desktop/hosts
but it didn't work.
结论
这是我想出了(基于帮助乡亲罚款给我)的code:
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
需要注意的是:
-
我指望总有要删除的域之前,标签
I am counting that there is always a tab before the domain to be erased
我自动转义域名点。
推荐答案
使用,sed的:
sed -i ".bak" '/culpa/d' test.txt
以上将删除包含的过失的在test.txt的线条。它会创建原始(名为test.txt.bak)的备份,并将修改就地原始文件。
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.
这篇关于巴什 - 查找文件中的关键字,并删除其行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!