如何在awk中包含挖掘查找?

如何在awk中包含挖掘查找?,awk,dig,Awk,Dig,我有一个awk命令从装载点提取信息(请参阅中的接受答案): 我想在这个awk命令中包含一个dig查找,以查找主机名的IP。不幸的是,mount命令有时包含IP,有时包含主机名。我尝试了以下操作,但它有一个不需要的换行符、不需要的返回代码,如果有IP地址,则无法工作: 主机名 echo "example.com:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +s

我有一个awk命令从装载点提取信息(请参阅中的接受答案):

我想在这个
awk
命令中包含一个dig查找,以查找主机名的IP。不幸的是,mount命令有时包含IP,有时包含主机名。我尝试了以下操作,但它有一个不需要的换行符、不需要的返回代码,如果有IP地址,则无法工作:

主机名

echo "example.com:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
echo "93.184.216.119:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
93.184.216.119 /remote/export /local/mountpoint
返回

93.184.216.119
0 /remote/export /local/mountpoint
适用于IP

echo "example.com:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
echo "93.184.216.119:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
93.184.216.119 /remote/export /local/mountpoint
返回

0 /remote/export /local/mountpoint
我想在这两种情况下检索以下内容

echo "example.com:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
echo "93.184.216.119:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
93.184.216.119 /remote/export /local/mountpoint
更新:

当IP作为查询提供时,
dig
的某些版本似乎返回IP,而其他版本则不返回任何内容

解决方案:

基于此,我使用了以下适应的
awk
命令:

 awk -F'[: ]' '{if(/^\//) { system("dig +short "$3" | grep . || echo "$3" | tr -d \"\n\""); print "",$4,$1 } else { system("dig +short "$1" | grep . || echo "$1" | tr -d \"\n\"");print "",$2,$4  };}'

附加的
grep.|echo“$3”
注意,如果dig不返回任何内容,则会返回输入IP/主机名。

awk中的
系统
命令执行命令,返回其状态。考虑这一点:

$ awk 'END { print "today is " system("date") " and sunny" }' < /dev/null
Tue Jan  7 20:19:28 CET 2014
today is 0 and sunny
为了防止换行符出现在
日期之后
,我们通过管道将其输出传输到
tr-d“\n”

长话短说,改变一下:

print system(...), $2, $4
为此:

system(... | tr -d \"\n\"); print "", $2, $4