我在文本文件中有这些数据。需要转换为放入模板
data.txt中
* A 11.22.33.44
mail A 11.22.33.44
domain1.com A 11.22.33.44
ftp CNAME domain1.com
www CNAME domain1.com
domain1.com MX mail.domain1.com
* A 55.66.77.88
mail A 55.66.77.88
domain2.com A 55.66.77.88
ftp CNAME domain2.com
www CNAME domain2.com
domain2.com MX mail.domain2.com
通过此vim
命令
:g/NS/d | :%s/CNAME/cnamerecord/g | :%s/A/arecord/g | :%s/MX/mxrecord/g
将其更改为:
arecord * 11.22.33.44
arecord mail 11.22.33.44
arecord domain1.com 11.22.33.44
ftp cnamerecord domain1.com
www cnamerecord domain1.com
domain1.com mxrecord mail.domain1.com
arecord * 55.66.77.88
arecord mail 55.66.77.88
arecord domain2.com 55.66.77.88
ftp cnamerecord domain2.com
www cnamerecord domain2.com
domain2.com mxrecord mail.domain2.com
现在我运行一个AWK命令
cat data.txt |
awk '{if ($2 == "arecord") print $2 " " $1 " " $3}
{if ($2 == "cnamerecord") print $2 " " $1 "." $3 " " $3}
{if ($2 == "mxrecord") print $2 " " $1 " " $3}'
,输出如下:
arecord * 11.22.33.44
arecord mail 11.22.33.44
arecord domain1.com 11.22.33.44
cnamerecord ftp.domain1.com domain1.com
cnamerecord www.domain1.com domain1.com
mxrecord domain1.com mail.domain1.com
arecord * 55.66.77.88
arecord mail 55.66.77.88
arecord domain2.com 55.66.77.88
cnamerecord ftp.domain2.com domain2.com
cnamerecord www.domain2.com domain2.com
mxrecord domain2.com mail.domain2.com
现在,这里有两个问题:
问题1和我最头疼的问题是:如何保持数据之间的空间。我想保留的文件中有一个空白行。
问题#2(奖励积分):在我的AWK代码中,我希望将arecord显示为:
arecord *.domain1.com 55.66.77.88
arecord mail.domain1.com 55.66.77.88
arecord domain1 55.66.77.88
NOT:
arecord * 11.22.33.44
arecord mail 11.22.33.44
arecord domain1.com 11.22.33.44
最终理想输出看起来像这样
arecord *.domain1.com 11.22.33.44
arecord mail.domain1.com 11.22.33.44
arecord domain1.com 11.22.33.44
cnamerecord ftp.domain1.com domain1.com
cnamerecord www.domain1.com domain1.com
mxrecord domain1.com mail.domain1.com
arecord *.domain2.com 55.66.77.88
arecord mail.domain2.com 55.66.77.88
arecord domain2.com 55.66.77.88
cnamerecord ftp.domain2.com domain2.com
cnamerecord www.domain2.com domain2.com
mxrecord domain2.com mail.domain2.com
我必须为150个域名执行此操作
任何帮助都会很棒
答案 0 :(得分:2)
有人这样吗?
cat file
arecord * 11.22.33.44
arecord mail 11.22.33.44
arecord domain1.com 11.22.33.44
ftp cnamerecord domain1.com
www cnamerecord domain1.com
domain1.com mxrecord mail.domain1.com
arecord * 55.66.77.88
arecord mail 55.66.77.88
arecord domain2.com 55.66.77.88
ftp cnamerecord domain2.com
www cnamerecord domain2.com
domain2.com mxrecord mail.domain2.com
awk命令
awk '
/arecord/ && !c {
c=NR+2}
NR==c {
c=0
print $1 " *." $2,$3 "\n" $1" mail." $2,$3 "\n" $0}
/^ftp|^www/{
print $2,$1 "." $3,$3}
/mxrecord/ {
print $2,$1,$3}
!NF {
print ""}
' file
arecord *.domain1.com 11.22.33.44
arecord mail.domain1.com 11.22.33.44
arecord domain1.com 11.22.33.44
cnamerecord ftp.domain1.com domain1.com
cnamerecord www.domain1.com domain1.com
mxrecord domain1.com mail.domain1.com
arecord *.domain2.com 55.66.77.88
arecord mail.domain2.com 55.66.77.88
arecord domain2.com 55.66.77.88
cnamerecord ftp.domain2.com domain2.com
cnamerecord www.domain2.com domain2.com
mxrecord domain2.com mail.domain2.com
更新以摆脱getline
答案 1 :(得分:2)
如果您将单独的if
语句加入if..else if..else
,则可以添加后备广告以按原样打印所有其他行;这将保留空行(以及任何注释行等):
cat data.txt | awk '{if ($2 == "arecord") print $2 " " $1 " " $3; else if ($2 == "cnamerecord") print $2 " " $1 "." $3 " " $3; else if ($2 == "mxrecord") print $2 " " $1 " " $3; else print}'