English 中文(简体)
Bash - 在档案中找到关键词并删除其行文[复制]
原标题:Bash - find a keyword in a file and delete its line [duplicate]

我愿作一个关键词,找到这一关键词在档案中拼凑一字,删除整个线。

这是我所说的话,但它并没有发挥应有的作用:

KEYWORD= domain.com 
cat /etc/hosts | grep -v "$KEYWORD" > /etc/hosts

<>>>>>

This is what worked for me:

ed -i_bak -e /domain.com/d /etc/hosts

然而,由于我有两条“主要.com”,因此,只有删除“重要......”的确切关键词的行文。

这是/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 ed -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.

http://www.ohchr.org。

这是我所起草的法典(根据优秀的民俗给予我的帮助):

D=domain.com
DOMAIN=`echo "$D" | sed  s/./\\./g `
sed -i_bak -e "/[	]$DOMAIN/d" /etc/hosts

注:

  • 我估计,要消除的领域总是有一个表象。

  • 我自动放弃域名的钥匙。

最佳回答
问题回答

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

除了对ed子的细微回答外,你还可以使用Perl和稍加改进的reg,以解决这个问题:

perl -pi.old -e  s/.*sdomain.coms*
//  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.

如果你只想删除你榜样档案的最后一段。

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




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签