slimjourney
slimjourney

Reputation: 103

Regular Expression - Replace multiple lines w/ same number of blank lines

Is it possible using regular expressions to to replace multiple lines w/ the same number of blank lines?

In context, I need to be able to change the following:

01    /*------------------------------------
02    Some history stuff goes here
03    Some history stuff goes here
04    Some history stuff goes here
05    Some history stuff goes here
06    Some history stuff goes here
07    --------------------------------------*/
08    int main void()
09    {
10       printf("Hello, World!");
11
12       return 0;
13    }

Into the following:

01    
02    
03    
04    
05    
06    
07    
08    int main void()
09    {
10       printf("Hello, World!");
11
12       return 0;
13    }

Right now I only have the following regex pattern, but it only replaces lines 01-07 with a single line:

\/\*.*?\*\/
(. matches newline checked)

If it helps, Im using Notepad++'s Find in Files feature since I need to do it in multiple files in a folder.

Upvotes: 1

Views: 199

Answers (2)

Toto
Toto

Reputation: 91518

This perl one-liner does the job:

perl -ne 'if (/\/\*/ .. /\*\//) {print "\n"} else {print}' file > output

under windows, change single quotes with double quotes

perl -ne "if (/\/\*/ .. /\*\//) {print \"\n\"} else {print}" file > output

Upvotes: 1

STIKO
STIKO

Reputation: 2095

Since you're using notpad++ I'm assuming you're on windows. You may need to use power shell to achieve this. First read the file line by line, found here

$reader = [System.IO.File]::OpenText("my.log")
while($null -ne ($line = $reader.ReadLine())) {
    $line
}

Then do some logic when the line starts with /* and ends with */, source here

$Group.StartsWith("/*")

Upvotes: 1

Related Questions