[英]Removing specific lines from file using bash script
I have a input file that contains DNS zone entries 我有一个包含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
How do I remove for example entries that belong to xxx subdomain domain? 例如,如何删除属于xxx子域域的条目?
Output should be 输出应为
; 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
I would like it to be in a bash script so I can call eg 我希望它在bash脚本中,所以我可以调用例如
delete_domain.sh xxx
Thanks. 谢谢。
One way: 单程:
Test file: 测试文件:
$ 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
$
Pattern file: 图案文件:
$ cat patterns
xxx
something-xxx
other-xxx
else.xxx
$
Awk option: Awk选项:
$ awk 'NR==FNR{a[$1];next}!(($1 in a) && $2=="IN" && $3=="A")' patterns file
abc IN A 000.000.000.000
$
First we load all the patterns to be removed into the associative array a. 首先,我们将所有要删除的模式加载到关联数组a中。 Then when the test file is processed, only those lines are printed whose 1st column is not a part of pattern and 2nd column is "IN" and 3rd column is "A".
然后,在处理测试文件时,仅打印第一行不是图案的一部分且第二列为“ IN”且第三列为“ A”的那些行。
Just a grep will do: 只需grep即可 :
grep -E -v 'xxx\s+IN\s+A' file
And to get rid of multiple empty lines, pipe that into: 为了摆脱多个空行,将其通过管道传递到:
cat -s
Put those in a bash script: 将它们放在bash脚本中:
$ cat test.sh
#!/bin/bash
grep -E -v 'xxx\s+IN\s+A' $1 | cat -s
and call it with: 并调用:
./test.sh file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.