Organizing grep with linebreaks

1

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.)

Benny

Posted 2017-10-09T02:05:59.757

Reputation: 113

Answers

2

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/'

Kamil Maciorowski

Posted 2017-10-09T02:05:59.757

Reputation: 38 429

0

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

davidgo

Posted 2017-10-09T02:05:59.757

Reputation: 49 152

This puts the space within each entry of the two lines where it should be connected instead of spaced – Benny – 2017-10-09T03:31:43.757

I don't understand what you are saying. I've updated my answe with the output I get. Can you update yours with the output you want ? – davidgo – 2017-10-09T03:35:34.423

I wonder if you are using a different OS and are running into a CR/LF difference - if thats all the problem is, you can pipe your output to "todos" or equivalent. – davidgo – 2017-10-09T03:41:01.143

Sorry for the confusion. I simply mean the linebreak should follow the 'MAC Address' line instead of the 'Nmap scan report' line – Benny – 2017-10-09T03:47:03.343