Removing Blank Lines in xml Document

1,352 views
Skip to first unread message

painterman

unread,
Jun 30, 2009, 6:11:09 PM6/30/09
to TextWrangler Talk
I hope I'm not becoming tiresome with my questions, but as a relative
newcomer, I really do appreciate all your help.

There is probably a simple way to do this, but I am trying to
automatically delete all blank lines in an xml document. For example;

<?xml version="1.0"?>

<!-- Painter 7.0 Brush Definition -->

<painter-brush-definition>

<format-control-parameters

version-number="7"


to become;

<?xml version="1.0"?>
<!-- Painter 7.0 Brush Definition -->
<painter-brush-definition>
<format-control-parameters
version-number="7"

One option I thought of trying in TextWrangler was the Process
Duplicate Lines option. The problem I have is finding a way of telling
the application that duplicates of a blank line are what I want to
delete. Any suggestions or alternative approaches would be very
welcome.

David

Tom Robinson

unread,
Jun 30, 2009, 6:20:12 PM6/30/09
to textwr...@googlegroups.com
On 2009-07-01, at 10:11, painterman wrote:

> There is probably a simple way to do this, but I am trying to
> automatically delete all blank lines in an xml document. For example;

In the Find & Replace dialog, Search For, hold down command and hit
return a couple of times. Move down to Replace With and hit command-
return once.

You should now be replacing \r\r (two returns, i.e. an end of line
followed by an empty line) with \r (one return).

You'll need to repeat multiple times to remove runs of more than one
blank line (or use a regular expression).

Steve Kalkwarf

unread,
Jul 1, 2009, 8:43:48 AM7/1/09
to textwr...@googlegroups.com
On Jun 30, 2009, at 6:11 PM, painterman wrote:

> One option I thought of trying in TextWrangler was the Process
> Duplicate Lines option. The problem I have is finding a way of telling
> the application that duplicates of a blank line are what I want to
> delete. Any suggestions or alternative approaches would be very
> welcome.

Choose "Process Duplicate Lines";
Click Matching all;
Select _only_ "Duplicates to new document";
Click "Process"

You'll find out there are no duplicates found, because "empty" lines
don't have duplicate content.

Luckily, Tom's Search & Replace tip will work well.

Steve

painterman

unread,
Jul 1, 2009, 8:54:05 AM7/1/09
to TextWrangler Talk
I am indebted to you again Tom. Your find and replace solution does
exactly what I wanted.

Below are 2 scripts I made to automatically strip all blank lines from
xml documents;

Remove Blank Lines;

tell application "TextWrangler"
activate
replace "\\r\\r" using "\\r" searching in text 1 of text document 1
options {search mode:literal, starting at top:false, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}
end tell

Remove Blank Lines_recursive (for all documents in the TextWrangler
Documents Drawer);

tell application "TextWrangler"
activate
replace "\\r\\r" using "\\r" searching in every text document options
{search mode:literal, starting at top:false, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}
end tell

By the way, are there any good books on AppleScript/ Regular
Expressions/ Grep that you would recommend for a beginner?

Many thanks

David

On Jun 30, 11:20 pm, Tom Robinson <barefootg...@tomrobinson.co.nz>
wrote:

painterman

unread,
Jul 1, 2009, 12:52:43 PM7/1/09
to TextWrangler Talk
Sorry, Steve, I did not see your post before replying to Tom.

Thank you for checking that.

David

painterman

unread,
Jul 1, 2009, 2:59:55 PM7/1/09
to TextWrangler Talk
Hey, I can also remove any lines containing tabs and line breaks
(which the previous script did not do), by using combinations of \t
and \r

I think I got a little out of step with the script below, so it is
possibly longer than necessary. But seems to work O.K, whist still
keeping the original tab spacing;


tell application "TextWrangler"
activate
replace "\\r\\r" using "\\r" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\r" using "" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\t\\r" using "" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\t\\t\\t" using "\\t\\t" searching in text 1 of text
document 1 options {search mode:literal, starting at top:true, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}

replace "\\t\\t\\t" using "\\t\\t" searching in text 1 of text
document 1 options {search mode:literal, starting at top:true, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}

replace "\\r\\r" using "\\r" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\r" using "" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\t\\r" using "" searching in text 1 of text document 1
options {search mode:literal, starting at top:true, wrap around:false,
backwards:false, case sensitive:false, match words:false, extend
selection:false}

replace "\\t\\t\\t\\t" using "\\t\\t" searching in text 1 of text
document 1 options {search mode:literal, starting at top:true, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}

replace "\\t\\t\\t" using "\\t\\t" searching in text 1 of text
document 1 options {search mode:literal, starting at top:true, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}
end tell

Cheers,

David
Reply all
Reply to author
Forward
0 new messages