user176569
user176569

Reputation: 1

awk input file has space space in quotes

Good day.

Trying to learn some AWK to convert some Juniper firewall configs to Cisco or Palo configs. Part of that is to parse the configuration. I have a sample here:

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 

I saved this snippet of lines to a file named test1 and ran this command from the command line:

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

Although it MOSTLY worked out, the spaces contained inside the " " where seen by awk as valid spaces. The output:

"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

Ideally, I would like to have awk ignore the spaces in the " ". I guess I could add it as a regular expression? Do I use the '!' somehow? Not sure. Any help would be appreciated.

Upvotes: 0

Views: 123

Answers (1)

l'L'l
l'L'l

Reputation: 47169

There are likely many ways to achieve your end result (maybe even one that is awk inclusive):

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

Another way that is possible is to use sed:

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

...or piped to awk:

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

Output:

"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
...

The awk solution was discovered after learning of this excellent example.

Upvotes: 1

Related Questions