Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing multiple blank lines with one blank line using RegEx search and replace

I have a file that I need to reformat and remove "extra" blank lines.

I am using the Perl syntax regular expression search and replace functionality of UltraEdit and need the regular expression to put in the "Find What:" field.

Here is a sample of the file I need to re-format.

 All current text  REPLACE with all the following:   Winter 2011 Class Schedule   Winter 2011 Class Registration Dates:  Dec. 6, 2010 – Jan. 1, 2011 Winter 2011 Class Session Dates:  Jan. 5 – Feb. 12, 2011  DANCE  Adventures in Ballet & Tap       3 – 6 years Instructor:  Ann Newby Tots ages 3 – 6 years old develop a greater sense of rhythm, flexibility and coordination as they explore the basic elements of movement. Saturdays   9 - 10 a.m.     Jan. 8 – Feb. 12        Six-week fees:   $30    African Storytelling 3 – 6 years Instructor:  Ann Newby Tots ages 3 – 6 years old explore storytelling and fables through spoken word, music, movement and visual arts experiences. Saturdays   10 – 11 a.m.    Jan. 8 – Feb. 12        Six-week fee:   $30   African Dance / Children 

You'll notice that some of the double blank lines have spaces or tabs or both in them.

After the search and replace has been run I should have a file that looks like this.

 All current text  REPLACE with all the following:  Winter 2011 Class Schedule   Winter 2011 Class Registration Dates:  Dec. 6, 2010 – Jan. 1, 2011 Winter 2011 Class Session Dates:  Jan. 5 – Feb. 12, 2011  DANCE  Adventures in Ballet & Tap       3 – 6 years Instructor:  Ann Newby Tots ages 3 – 6 years old develop a greater sense of rhythm, flexibility and coordination as they explore the basic elements of movement. Saturdays   9 - 10 a.m.     Jan. 8 – Feb. 12        Six-week fees:   $30   African Storytelling 3 – 6 years Instructor:  Ann Newby Tots ages 3 – 6 years old explore storytelling and fables through spoken word, music, movement and visual arts experiences. Saturdays   10 – 11 a.m.    Jan. 8 – Feb. 12        Six-week fee:   $30  African Dance / Children 
like image 944
Art Avatar asked Dec 17 '10 21:12

Art


People also ask

How do you match a blank line in regex?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.

How do you replace blank lines?

Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box. Click the Replace All button to replace all blank lines.


1 Answers

Replacing

^(\s*\r\n){2,}

With

\r\n

Is what I ended up with.

This only selects blank lines in multiples of two or more and replaces them with one.

like image 169
Art Avatar answered Sep 20 '22 17:09

Art