Using grep and cut I have returned the IP and ports of the nmap scan result which looks like this:

Desired output:
  • 1
    It would be useful to see the shape of your starting point. – roaima Mar 5 '19 at 8:46
  • Hey roaima you can find my starting point here initial post – KIllua Mar 5 '19 at 8:52
  • Posting on multiple sites is off-topc for U&L. If you want to delete your original post then then one becomes on-topic. – roaima Mar 5 '19 at 9:01
  • 1
    duly noted roaima – KIllua Mar 5 '19 at 9:03
awk '/\./{ip = $0; next}; {print ip":"$0}'

should be enough on that input.

Note that nmap has options to output in formats suitable for programmatic processing like xml.

| improve this answer | |

You can do something as the following using awk:

awk '$1 ~ /[0-9]\.[0-9]+/{ip=$1; val=0; next } /^[0-9]+$/{port=$1; if(!(val++)); print ip":"port}' infile
| improve this answer | |

One solution with a bash script:

for i in $(<input); do [ ${#i} -le 3 ] && printf '%s:%s\n' $ip $i || ip=$i; done

This saves the ip address in a variable if the input string length is greater than 3 characters and prints ip:port otherwise. Replace input with your input file. (You can choose a greater value to match if your port numbers have more than 3 characters)

Another one:

while read i; do [ ${i/.} != $i ] && ip=$i || printf "%s:%s\n" $ip $i; done <input

This assigns the ip address to variable ip if it contains a dot and prints ip:port otherwise.

| improve this answer | |

I have done by below method

sed -n '/^[0-9]\{3\}\./,+1p' filename| sed "N;s/\n/:/g"

| improve this answer | |
  • 1
    The output is missing port 443 – RalfFriedl Mar 5 '19 at 17:38
  • Will check and update – Praveen Kumar BS Mar 5 '19 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.