0

I have this command which works well to give me a list of IP addresses, MAC addresses, and MAC vendor, sorted by IP address.

sudo nmap -sn 192.168.103.0/24 | awk '/Nmap scan report for/{printf $5;}/MAC Address:/{print ","substr($0, index($0,$3)) }' | sort -t . -k 4,4n

All I need to do is change my awk print statement so that it puts a comma between the 2nd and 3rd column. So basically I will end up with 3 columns, column 1 is IP address, column 2 is MAC address, column 3 is MAC vendor name, with all rows sorted by IP address. I got everything I want except the comma between the 2nd and 3rd column. This will be a CSV file for input into a database.

1

1 Answer 1

1

A simple solution will be to pipe the result to another awk .

nmap -sn 192.168.103.0/24 | \
    awk '/Nmap scan report for/{printf $5;}/MAC Address:/{print ","substr($0, index($0,$3)) }' | \
    awk '{ print $1","$2" "$3" "$4" "$5" "$6 }' | tr -d '()'  | sort -t . -k 4,4n

The begin of this command is the same i added only awk '{ print $1","$2" "$3" "$4" "$5" "$6 }' to add the second coma

And tr -d '()' is here to remove parenthesis arround the Mac Vendor .

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .