Trying to remove all empty lines with this grep isn't working

406 views
Skip to first unread message

David J

unread,
Apr 26, 2021, 7:37:04 PM4/26/21
to BBEdit Talk

 I'm trying to take out any empty lines from a document and can't figure out why it's not working.
I'm adding the text, then selecting all then adding grep
^\s*?\r     
then replace all with nothing. I'm getting "not found" on any document. Weirdly, I swear this was working before. Can anyone give some insight here?
 

Tom Robinson

unread,
Apr 26, 2021, 7:53:32 PM4/26/21
to BBEdit Talk
If you’re wanting to remove empty lines, any reason you’re not searching for \r\r and replacing with \r ?

jj

unread,
Apr 27, 2021, 4:38:38 AM4/27/21
to BBEdit Talk
Hi David,

Can it be that your file has Unix line breaks and you are trying to match carriage returns?

There are 3 forms of line breaks:

• Unix (LF - linefeed) that can be matched with \n
• Legacy Mac (CR - carriage return) that can be matched with \r
• Windows (CRLF) that can be matched with \r\n

Furthermore PCRE regular expressions have the following generic character types:

 • \s any white space character
 • \h any horizontal white space character
 • \v any vertical white space character


This regex should match any number of contiguous empty lines for the 3 types of line breaks:

    ^\s*\v 
    
And those, single empty lines:

    ^\s*?\v  (your initial intent)
or
    ^\h*\v
    
HTH

Jean Jourdain

Christopher Stone

unread,
Apr 27, 2021, 8:11:09 AM4/27/21
to BBEdit-Talk


Hey David,

Why are you selecting-all?

Adding grep where?

Your pattern above works with normal text and blank lines.

Although I would use a capital R to represent more types of line endings:

^\s*?\R

The fact nothing is working for you might be explained by:

1) You've turned OFF [ ] Grep in the Find dialog.

2) You are selecting-all and then using Cmd-G to try to find without Wrap-Around active.

Another thing – blank lines are often NOT blank.  Very frequently some whitespace has crept in that needs to be accounted for, and it is also common for normal lines to have accidental trailing whitespace.

So – I would manage this be removing any trailing whitespace and blank lines at the same time:

Find:

\h*\v\s+

Replace:

\n

--
Best Regards,
Chris

Patrick Woolsey

unread,
Apr 27, 2021, 11:18:30 AM4/27/21
to bbe...@googlegroups.com
On 4/27/21 at 4:38 AM, goo...@mixio.com (jj) wrote:

>Can it be that your file has Unix line breaks and you are
>trying to match carriage returns?
>
>There are 3 forms of line breaks:
>
>• Unix (LF - linefeed) that can be matched with \n
>• Legacy Mac (CR - carriage return) that can be matched with \r
>• Windows (CRLF) that can be matched with \r\n


As a reminder, you needn't worry about matching all possible
line ending conventions in your BBEdit search patterns; \n
alone will suffice in essentially every instance.


Regards

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

Patrick Woolsey

unread,
Apr 27, 2021, 11:28:35 AM4/27/21
to bbe...@googlegroups.com
On 4/26/21 at 7:37 PM, future...@gmail.com (David J) wrote:

>I'm trying to take out any empty lines from a document and
>can't figure out why it's not working. I'm adding the text,
>then selecting all then adding grep
>
>^\s*?\r
>then replace all with nothing. I'm getting "not found" on any document.


Since your above pattern should suffice to remove all blank
lines (lines which are empty or contain only whitespace), please
turn on the "Show matches" search option as well as View -> Text
Display -> Show Invisibles in hopes these may shed some light on
the matter.

If it doesn't, then a Zipped sample file is probably in order.

[PS: You need not select anything before performing the Replace
All _unless_ you need to limit the scope of this command to a
specific part of the document via the "Search in: Selected text
only" option.]

David J

unread,
Apr 27, 2021, 11:48:41 AM4/27/21
to BBEdit Talk
Hey all,  It turns out that all I had to do was capitalize the R at the end as mentioned above. I had the recipe in notes from years ago and was just following what I wrote down then. My regex and bbedit understanding is very rusty so appreciate everyone chiming in. David
Reply all
Reply to author
Forward
0 new messages