Linux 使用bash脚本从文件中删除特定行

Linux 使用bash脚本从文件中删除特定行,linux,bash,sed,Linux,Bash,Sed,我有一个包含DNS区域条目的输入文件 ; whatever.foo [000000] $ORIGIN whatever.foo. $TTL 8400 @ IN SOA ns1.whatever.foo. admin.whatever.foo. ( 2017101913 ; serial number 21600 ; refresh 8400

我有一个包含DNS区域条目的输入文件

; whatever.foo [000000]
$ORIGIN whatever.foo.
$TTL 8400
@       IN      SOA     ns1.whatever.foo.      admin.whatever.foo. (
2017101913 ; serial number
                    21600                    ; refresh
                    8400                     ; retry
                    1209600                 ; expire
                    8400                    ; ttl
                    )

xxx           IN    A    000.000.000.000
something-xxx IN    A    000.000.000.000
other-xxx     IN    A    000.000.000.000
else.xxx      IN    A    000.000.000.000

yyy           IN    A    000.000.000.001
something-yyy IN    A    000.000.000.001
other-yyy     IN    A    000.000.000.001
else.yyy      IN    A    000.000.000.001
如何删除属于xxx子域的示例条目

输出应该是

; whatever.foo [000000]
$ORIGIN whatever.foo.
$TTL 8400
@       IN      SOA     ns1.whatever.foo.      admin.whatever.foo. (
2017101913 ; serial number
                    21600                    ; refresh
                    8400                     ; retry
                    1209600                 ; expire
                    8400                    ; ttl
                    )

yyy           IN    A    000.000.000.001
something-yyy IN    A    000.000.000.001
other-yyy     IN    A    000.000.000.001
else.yyy      IN    A    000.000.000.001
我希望它是在一个bash脚本,所以我可以调用例如

delete_domain.sh xxx
谢谢。

单程:

测试文件:

$ cat file
xxx           IN    A    000.000.000.000
else.xxx      IN    A    000.000.000.000
something-xxx IN    A    000.000.000.000
other-xxx     IN    A    000.000.000.000
abc           IN    A    000.000.000.000
$
模式文件:

$ cat patterns
xxx
something-xxx
other-xxx
else.xxx
$
Awk选项:

$ awk 'NR==FNR{a[$1];next}!(($1 in a) && $2=="IN" && $3=="A")' patterns file
abc           IN    A    000.000.000.000
$
首先,我们将要删除的所有模式加载到关联数组a中。然后,当处理测试文件时,只打印第一列不是图案的一部分、第二列在中、第三列是a的行。

只需grep即可:

要消除多条空行,请将其导入:

cat -s
将它们放在bash脚本中:

$ cat test.sh
#!/bin/bash

grep -E -v 'xxx\s+IN\s+A' $1 | cat -s
并称之为:

./test.sh file

看起来太复杂了。你可以发布初始输入内容和预期输出,这样我们就可以建议最佳解决方案,不是吗?初始输入是一个区域文件,其条目如上所示。输出是一个区域文件,其中删除了这些条目。另请参见编辑。@whitetrash只需发布输入文件或其中的一部分以及该文件的预期输出。确定。。。这是糟糕的一天。。。当代码运行时,这个问题毫无意义——它并没有被使用。我将重写它,使其符合以下答案:我的坏:
./test.sh file