awk输入文件的引号中有空格

时间:2014-10-22 23:55:06

标签: regex awk

美好的一天。

尝试学习一些AWK,将一些瞻博网络防火墙配置转换为思科或Palo配置。部分原因是解析配置。我在这里有一个样本:

set service "RDP" protocol tcp src-port 0-65535 dst-port 3389-3389 
set service "LDAPS" protocol tcp src-port 0-65535 dst-port 636-636 
set service "SOAPS" protocol tcp src-port 0-65535 dst-port 444-444 
set service "KEYS-ADMIN" protocol tcp src-port 0-65535 dst-port 9000-9000 
set service "WSUS-MDM" protocol tcp src-port 0-65535 dst-port 8530-8530 
set service "WSUS-MDM" + tcp src-port 0-65535 dst-port 8531-8531 
set service "WSUS-MDM" + tcp src-port 0-65535 dst-port 8531-8531 
set service "HTTPS-MDM" protocol tcp src-port 0-65535 dst-port 8443-8443 
set service "IPSEC - 4500" protocol udp src-port 0-65535 dst-port 4500-4500 
set service "IPSEC - 4500" + tcp src-port 0-65535 dst-port 1433-1433 
set service "IPSEC - 4500" + tcp src-port 0-65535 dst-port 1433-1433 
set service "OKFTP" protocol tcp src-port 0-65535 dst-port 2169-2169 
set service "Bomgar 8200" protocol tcp src-port 0-65535 dst-port 8200-8200 
set service "Cisco VPN" protocol tcp src-port 0-65535 dst-port 10000-10000 
set service "Cisco VPN 2" protocol tcp src-port 0-65535 dst-port 10000-10000 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 10000-10000 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 500-500 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 4500-4500 
set service "Cisco VPN 2" + 50 src-port 0-65535 dst-port 0-65535 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 10000-10000 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 500-500 
set service "Cisco VPN 2" + udp src-port 0-65535 dst-port 4500-4500 
set service "TrendMicro8080" protocol tcp src-port 0-65535 dst-port 8080-8080 
set service "TrendMicro26980" protocol tcp src-port 0-65535 dst-port 26980-26980 
set service "TrendMicro26980" + udp src-port 0-65535 dst-port 26980-26980 
set service "PenPal Test" protocol tcp src-port 0-65535 dst-port 522-522 
set service "HTTP8080" protocol tcp src-port 0-65535 dst-port 8080-8080 
set service "HTTPS445" protocol tcp src-port 0-65535 dst-port 445-445 
set service "MOBILEIRON-TLS" protocol tcp src-port 0-65535 dst-port 9997-9997 
set service "MOBILEIRON-TLS" + tcp src-port 0-65535 dst-port 9998-9998 

我将这段行保存到名为test1的文件中,并从命令行运行此命令:

awk -F " " 'BEGIN {OFS=","} {print $3,$5,$7,$9}' test1

虽然它已经解决了,但是" " awk将其视为有效空间。输出:

"RDP",tcp,0-65535,3389-3389
"LDAPS",tcp,0-65535,636-636
"SOAPS",tcp,0-65535,444-444
"KEYS-ADMIN",tcp,0-65535,9000-9000
"WSUS-MDM",tcp,0-65535,8530-8530
"WSUS-MDM",tcp,0-65535,8531-8531
"WSUS-MDM",tcp,0-65535,8531-8531
"HTTPS-MDM",tcp,0-65535,8443-8443
"IPSEC,4500",udp,0-65535
"IPSEC,4500",tcp,0-65535
"IPSEC,4500",tcp,0-65535
"OKFTP",tcp,0-65535,2169-2169
"Bomgar,protocol,src-port,dst-port
"Cisco,protocol,src-port,dst-port
"Cisco,2",tcp,0-65535
"Cisco,2",udp,0-65535
"Cisco,2",udp,0-65535
"Cisco,2",udp,0-65535
"Cisco,2",50,0-65535
"Cisco,2",udp,0-65535
"Cisco,2",udp,0-65535
"Cisco,2",udp,0-65535

理想情况下,我想让awk忽略"中的空格。 &#34 ;.我想我可以把它添加为正则表达式?我是否使用'!'不知何故?不确定。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

可能有很多方法可以达到你的最终结果(也许甚至包括awk的那个):

awk -F\" 'BEGIN {OFS=","} {split($3,F," ");print $2,F[2],F[4],F[6]}' test1

另一种可能的方法是使用sed

sed 's/\("[^"]*"\)* \("[^"]*"\)*/\1,\2/g' test1

...或通过管道传递给awk

sed 's/\("[^"]*"\)* \("[^"]*"\)*/\1,\2/g' test1 | awk -F ',' 'BEGIN {OFS=","} {print $3,$5,$7,$9}'

<强>输出:

"RDP",tcp,0-65535,3389-3389
"LDAPS",tcp,0-65535,636-636
"SOAPS",tcp,0-65535,444-444
"KEYS-ADMIN",tcp,0-65535,9000-9000
"WSUS-MDM",tcp,0-65535,8530-8530
"WSUS-MDM",tcp,0-65535,8531-8531
"WSUS-MDM",tcp,0-65535,8531-8531
"HTTPS-MDM",tcp,0-65535,8443-8443
"IPSEC - 4500",udp,0-65535,4500-4500
"IPSEC - 4500",tcp,0-65535,1433-1433
"IPSEC - 4500",tcp,0-65535,1433-1433
"OKFTP",tcp,0-65535,2169-2169
"Bomgar 8200",tcp,0-65535,8200-8200
"Cisco VPN",tcp,0-65535,10000-10000
"Cisco VPN 2",tcp,0-65535,10000-10000
"Cisco VPN 2",udp,0-65535,10000-10000
"Cisco VPN 2",udp,0-65535,500-500
...

在了解this excellent example之后发现了awk解决方案。