Putting a string on same line tcl

849 Views Asked by At

I have a nmap output and I need to put strings on different lines on same line.

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

New Ouput:

169.254.0.1,Voip adapter

How can I do this on tcl or bash?

2

There are 2 best solutions below

0
On BEST ANSWER

In Tcl, we can use regexp to extract the required data.

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
}
0
On

Brute force:

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