Organizing grep with linebreaks

How can I add linebreaks between my egrep?

# nmap -sP 192.168.1.0/24 | egrep 'MAC|report'

I would like to add a linebreak after each of the two lines. What is the most efficient shorthand?

Essentially I'd like the result to be output like so:

Nmap scan report for 192.168.1.7
MAC Address: C4:42:02:xx:xx:xx (Samsung Electronics Co.)
Nmap scan report for 192.168.1.8
MAC Address: 04:F1:3E:xx:xx:xx (Apple)
Nmap scan report for 192.168.1.10
MAC Address: 70:18:8B:xx:xx:xx (Hon Hai Precision Ind. Co.)

2 Answers

I would like to add a linebreak after each of the two lines.

The solution to this literal problem is here. In your case:

nmap -sP 192.168.1.0/24 | egrep 'MAC|report' | sed '0~2 s/$/\n/g'

However I've seen nmap outputs where some reports miss their MAC line, so (instead of blindly counting lines) you'd rather want a newline before every Nmap, unless it's in the very first line:

nmap -sP 192.168.1.0/24 | egrep 'MAC|report' | sed '1! s/^Nmap/\nNmap/'

I came up with

nmap -sP 192.168.10/24 | egrep 'MAC|report|Host is up' | sed '/Host is/c\\r'

This searches for the additional string "Host is up" and then replaces it with a carriage return.

This results in the following output:

Nmap scan report for 192.168.1.1
MAC Address: 00:0E:C6:C7:93:38 (Asix Electronics)
Nmap scan report for (192.168.1.254)
MAC Address: 1C:C1:DE:80:53:55 (Hewlett Packard)
Nmap scan report for 192.168.1.250
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like