Justin Lund Justin Lund - 9 months ago 38
Linux Question

Add blank line before a certain phrase in a text file in Linux?

I'm using Kali Linux, trying to sort out some input from Nmap. Basically, I ran a scan from NMap, and need to extract specific pieces of information from it. I've got it to show everything I need using the following command:

cat discovery.txt | grep 'Nmap scan report for\|Service Info: OS:\|OS CPE:\|OS guesses:\|OS matches\|OS details'

Essentially, each section of information I need will start with "Nmap scan report for [IP ADDRESS]"

I'd like to add to my command to have it create a blank line before every appearance of the word "Nmap", to clearly separate each chunk of information.

Is there any command I can use to do this?


Sure, you can use Perl.

perl -pe 's/^Nmap/\nNmap/'