将字符串放在同一行 tcl

Putting a string on same line tcl

我有一个 nmap 输出,我需要将字符串放在同一行的不同行上。

Nmap 输出:

 Nmap scan report for 169.254.0.1
 Host is up (0.014s latency).
 Not shown: 97 closed ports
 PORT     STATE SERVICE
 80/tcp   open  http
 1720/tcp open  H.323/Q.931
 5060/tcp open  sip
 Device type: VoIP adapter|WAP|PBX|webcam|printer

新输出:

169.254.0.1,Voip 适配器

我如何在 tcl 或 bash 上执行此操作?

暴力破解:

<your_nmap_output> |  \
egrep "Nmap scan report|Device type" | \
sed -r 's/[ ]*Nmap scan report for (.*)$/,/' | \
sed -r 's/[ ]*Device type: ([^\|]*)\|.*//' |  \
xargs

Tcl中,我们可以使用regexp来提取需要的数据。

set nmap_output "Nmap scan report for 169.254.0.1
 Host is up (0.014s latency).
 Not shown: 97 closed ports
 PORT     STATE SERVICE
 80/tcp   open  http
 1720/tcp open  H.323/Q.931
 5060/tcp open  sip
 Device type: VoIP adapter|WAP|PBX|webcam|printer"

if {[regexp {scan\s+report\s+for\s+(\S+).*Device\s+type:\s+([^|]+)} $nmap_output match ip type]} {
    puts $ip,$type
}