Remove empty lines in text using Visual Studio.

Visual Studio has ability to delete empty lines in replace operation using regular expressions.
1.Click Ctrl-H (quick replace)
2. Tick "Use Regular Expressions"
3. In Find specify ^$\n
4. In Replace box delete everything.
5 Click "Replace All"
All empty lines will be deleted.

 Regular expression for empty line consist of   

Beginning of line ^

 

 

End of line $

 

Line break \n

 

Note that normally in Windows an end of line  indicated by 2 characters CRLF - Carriage Return (CR, ASCII 13, \r) Line Feed (LF, ASCII 10, \n).

Addition from  Kenneth at 6/18/2009:
A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):   ^:b*$\n

Addition from   Igor  at 8/5/2011:

To remove double lines:  ^:b*\n:b*\n replace with:  \n

 

To delete  rows with single comma

3. In Find specify ^,$\n

 

For similar requirement to remove comma at the end of lines in text, do the following steps.


1.Click Ctrl-H (quick replace)
2. Tick "Use Regular Expressions"
3.
 In Find specify ,\r?\n.
4. In Replace box type \n

5 Click "Replace All"

 

 

posted @ Tuesday, March 4, 2008 4:28 PM
Print

Comments on this entry:

# re: Remove empty lines in text using Visual Studio.

Left by Rob at 5/15/2008 4:12 AM
Gravatar
Cool, thanks!

# re: Remove empty lines in text using Visual Studio.

Left by felipe at 5/30/2008 1:18 AM
Gravatar
Oh man i've been asking myself how to do this for years :D
thanks for the neat tip! this helped me a lot!!

# re: Remove empty lines in text using Visual Studio.

Left by Asbjorn at 7/31/2008 3:37 AM
Gravatar
Thanks alot :)

VS added 350.000 empty lines in one of my files..

# re: Remove empty lines in text using Visual Studio.

Left by Pete at 9/6/2008 4:34 AM
Gravatar
thanks! could you explain the regex? why is the newline character after the end of line marker?

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 9/6/2008 2:42 PM
Gravatar
Pete,
I've added more info about regEx into my post.

# re: Remove empty lines in text using Visual Studio.

Left by Sung at 10/2/2008 9:53 AM
Gravatar
thanks for the tips.
after playing with it, i find \t\n with wildcards configuration do the works too.

# re: Remove empty lines in text using Visual Studio.

Left by Ross Youngblood at 10/20/2008 8:50 AM
Gravatar
This doesn't work in Visual Studio 6.0 (As far as I could tell.) Yes we are stuck in the dark ages.
I suppose I could download Emacs... and be done with this problem.

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 10/20/2008 10:06 PM
Gravatar
Ross,
You also can download Visual Studio 2008 Express, which is a good editor (even if you have to compile in Visual Studio 6.0)

# re: Remove empty lines in text using Visual Studio.

Left by shophola at 4/27/2009 3:45 PM
Gravatar
cool! tips :D

# re: Remove empty lines in text using Visual Studio.

Left by Gyanendra Kumar Mishra at 5/18/2009 2:47 PM
Gravatar
Thanks really this code is a life saver

# re: Remove empty lines in text using Visual Studio.

Left by Kenneth at 6/18/2009 3:28 PM
Gravatar
I would offer an addendum:

A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):

^:b*$\n

# re: Remove empty lines in text using Visual Studio.

Left by Steven Chalk at 8/23/2009 6:18 AM
Gravatar
simple when you see the answer but I wasted and hour or so trying combinations until I found this solution. Well done.

# re: Remove empty lines in text using Visual Studio.

Left by samy at 12/1/2009 11:49 AM
Gravatar
well. it is a lifesaver. i think the first line should be changed to ctrl+H instead of ctrl+D as mentioned

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 12/2/2009 5:44 AM
Gravatar
samy, thanks, it's indeed ctrl-H

# re: Remove empty lines in text using Visual Studio.

Left by Haidar at 12/3/2009 6:55 AM
Gravatar
Great solution in short.

Thanks,
Haidar

# re: Remove empty lines in text using Visual Studio.

Left by Vaishnavi at 12/22/2009 2:54 AM
Gravatar
Thank you. helped me in formatting code which I got it from the net. Worked in a jiffy

# re: Remove empty lines in text using Visual Studio.

Left by Nouman Zakir at 3/23/2010 11:25 AM
Gravatar
thank you so much, i hate those blank lines when ever i paste some code from website or notepad. thanks to your tip i will never see those lines again.

# re: Remove empty lines in text using Visual Studio.

Left by Sagar at 3/31/2010 8:56 AM
Gravatar
ultimate..
it workss..!!
really thanks

# re: Remove empty lines in text using Visual Studio.

Left by mhd at 3/31/2010 11:08 PM
Gravatar
wow! why i didn't think of that before?!?
I think the 'magic' is in the $\n .
So,from now I will use $\n everytime I want to mass delete certain lines without leaving blank line trace. Thanks!!

# re: Remove empty lines in text using Visual Studio.

Left by danbo at 5/10/2010 4:22 PM
Gravatar
Kenneth, perfect for cleaning up the mess VS sometimes makes of the html.

thanks,

danbo

# re: Remove empty lines in text using Visual Studio.

Left by ELemus at 5/25/2010 2:20 PM
Gravatar
Gracias por la solución (Tanx 4 the solution) Great

# re: Remove empty lines in text using Visual Studio.

Left by Roman at 5/28/2010 8:09 AM
Gravatar
Thanks!!! 8))

# re: Remove empty lines in text using Visual Studio.

Left by Ed at 11/11/2010 1:55 PM
Gravatar
Thanks for this post. I found the resulting no lines a bit difficult to read.

I used

Search for: ^$\n+
Replace With:\n

# re: Remove empty lines in text using Visual Studio.

Left by mt at 1/25/2011 6:38 AM
Gravatar
Thanks A lot kenneth.

# re: Remove empty lines in text using Visual Studio.

Left by Anthony at 1/31/2011 3:48 PM
Gravatar
What about going the other way and inserting carriage returns?

tried replacing with \n or \r

predictably just adds '\n'

# re: Remove empty lines in text using Visual Studio.

Left by Anthony at 1/31/2011 3:51 PM
Gravatar
Just make sure regular expression check box in the options is checked, then it should work

# re: Remove empty lines in text using Visual Studio.

Left by EvilDr at 2/9/2011 2:53 AM
Gravatar
724 occurrence(s) replaced - in 0 seconds!
Dude that is one awesome tip :-D

# re: Remove empty lines in text using Visual Studio.

Left by Tim at 6/30/2011 10:41 AM
Gravatar
works perfectly thanks!

# re: Remove empty lines in text using Visual Studio.

Left by Igor at 8/5/2011 11:08 AM
Gravatar
Great, I modifyed your code to remove double lines:
^:b*\n:b*\n
replace with:
\n
Worked like a charm!

# re: Remove empty lines in text using Visual Studio.

Left by Srinu at 8/11/2011 11:33 PM
Gravatar
Thank u ...For this post...

# re: Remove empty lines in text using Visual Studio.

Left by logicalweb at 8/12/2011 5:23 AM
Gravatar
Thank u ...For this post...
even i've been asking myself how to do this for years :D

# re: Remove empty lines in text using Visual Studio.

Left by mice from space at 10/3/2011 6:05 PM
Gravatar
GREAT JOB!!!!

# re: Remove empty lines in text using Visual Studio.

Left by Matthew at 3/4/2012 5:59 PM
Gravatar
This no longer works in VS 11, Leaving the Replace with field blank just makes it insert an emptly line. Any ideas?

# re: Remove empty lines in text using Visual Studio.

Left by Ryan Martin at 11/17/2012 9:36 AM
Gravatar
This is very similar to what I am looking for. Can someone please help me out with the following RegEx pattern to find the following code in my JavaScript file.

I'm looking for all of the jQuery $("#some-element-by-id") or $(".some-element-by-class") references that do not contain either a # or . inside them which is breaking my Replace function. I have a 6000 line JavaScript file so its been tough to locate the culprit.

I tried the following with no such luck.
$("[^#]
$("[^.]
$("[^#*]")
$("[^.*]")

# re: Remove empty lines in text using Visual Studio.

Left by michael freidgeim at 11/17/2012 9:33 PM
Gravatar
Ryan,
You don't need to use regular expression. Just search for $(" as string, (not regular expressions) to output all relevant records to "search results" window.
Then copy output to a new file and in this file replace all occurrences of $(". And $("# with empty strings. Remaining rows with $(" will be what are you looking for.

# re: Remove empty lines in text using Visual Studio.

Left by thehill at 1/12/2013 9:56 AM
Gravatar
great post, thanks, it set me on the right track

my objective was to remove surplus blank lines within vb code window, i.e multiple blank lines(including tabs and spaces) would all be reduced to one empty line (no spaces and no tabs)

i was able to run the double lines example recursively and it gave me the result that i was after. Or at least i thought it did until i opened my files back up and studio was complaining about inconsistent line endings, i think we ended up with LF when it was looking for CRLF

i propose the following which resolves both of these issues
find ^{:b*\n}+:b*\n
replace \x000D\x000A

^ start of line
{:b*\n}+ one or more lines containing only zero or more whitespace chars
:b*\n exactly one line containing only zero or more whitespace chars

\x000D\x000A unicode identifiers for CR followed by LF

i can now run this against the whole solution and achieve exactly what i want in one step

# re: Remove empty lines in text using Visual Studio.

Left by a at 7/24/2014 10:16 PM
Gravatar
a

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456