Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Want to remove multiple occurances of blank line from a file

3,169 views
Skip to first unread message

Puneet

unread,
Apr 22, 2005, 7:09:07 AM4/22/05
to
Hi All,

I want to do a small thing. I have a file "file.txt"

The file is like below.

"my name is abc

asklda
addas
asdasda

asdas

"

Here in above file i want to replace multiple "\n" with only one "\n".
I mean i want to get the file like this.....

"my name is abc

asklda
addas
asdasda

asdas
"

So what should i do, for replacing the "\n" character.

-- Puneet

Christopher Nelson

unread,
Apr 22, 2005, 7:49:30 AM4/22/05
to
Puneet wrote:
> Hi All,
>
> I want to do a small thing. I have a file "file.txt"
> ...

> Here in above file i want to replace multiple "\n" with only
> one "\n".
> ...

> So what should i do, for replacing the "\n" character.

That could depend a lot on how big the file is. If it's relatively
small, you could read it all into one string and then change all
instances of \n\n to \n with [regsub] or [string map]. If the file is
large enough that you have to process it a line at a time, a different
approach would be needed.

Chris

Puneet

unread,
Apr 22, 2005, 7:55:36 AM4/22/05
to
Yes my file is large one....near about 1500 lines.

And i tried this expression.

regsub -all {\n\n[\n]*} $data "\n" data

But this not successful because there can be space or tabs or blank in
new line.

--Puneet

Don Porter

unread,
Apr 22, 2005, 9:24:58 AM4/22/05
to
Puneet wrote:
> Yes my file is large one....near about 1500 lines.

Impossible. No computer can hold a file so large as that.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

lvi...@gmail.com

unread,
Apr 22, 2005, 9:53:59 AM4/22/05
to

According to Puneet <mr.pune...@gmail.com>:
:regsub -all {\n\n[\n]*} $data "\n" data

:
:But this not successful because there can be space or tabs or blank in
:new line.

Then would something more like be of help?
regsub -all {\s*\n\s*\n(\s*\n)*} $data "\n" data

--
<URL: http://wiki.tcl.tk/ > MP3 ID tag repair < http://www.fixtunes.com/?C=17038 >
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvi...@gmail.com > <URL: http://www.purl.org/NET/lvirden/ >

Helmut Giese

unread,
Apr 22, 2005, 10:13:48 AM4/22/05
to
On 22 Apr 2005 04:55:36 -0700, "Puneet" <mr.pune...@gmail.com>
wrote:

How about
regsub -all {(\s*\n)+} $data "\n" data
HTH
Helmut Giese

Larry W. Virden

unread,
Apr 22, 2005, 10:27:19 AM4/22/05
to
Here's something pecular.
Tcl 8.5a3:
% set data {
line 1

line 3


line 6

line 8

}

line 1

line 3


line 6

line 8


% regsub -all {\s*\n\s*\n(\s*\n)*} $data "\n" data
4
% puts $data

line 1
line 3
line 6
line 8

%

What's with the addition of that
extra newline above? And shouldn't that last newline newline combo been
converted as well?

Donal K. Fellows

unread,
Apr 22, 2005, 10:58:52 AM4/22/05
to
Larry W. Virden wrote:
> What's with the addition of that
> extra newline above? And shouldn't that last newline newline combo been
> converted as well?

The first character is a newline, and the RE will not actually remove
newline sequences, but only shorten them to a single newline. Getting
rid of the extra newlines could be as simple a thing as just doing this:

string trim [regsub -all {\n(?:\s*\n)+} $data \n] \n

But I prefer to use a different approach that involves choosing the
things I want and not the ones I don't:

join [regexp -all -inline {(?=[^\n]*\S)[^\n]+} "$data \n \n foo"] \n

Donal.

Khaled

unread,
Apr 22, 2005, 8:14:29 PM4/22/05
to


Since you're reading from a file (and writing to another), the *faster*
in this case is to use [gets]. Here is an example:

proc noEmptyLines {inFile outFile} {
set inCh [open inFile]
set outCh [open outFile w]

while {[gets $inCh line] != -1} {
if {$line eq ""} {continue}
# if {[string match $line ""]} {continue}
# in case you are not using Tcl8.4
puts $outCh $line
}

close $inCh
close $outCh
}

noEmptyLines file.txt new_file.txt

This will be _very_ fast.

Good Day.. Khaled

Puneet

unread,
Apr 22, 2005, 11:14:12 PM4/22/05
to
Hi All,

Thanks a lot to participate in this thread. And i know this is really
worthful and askable question. I tried all the suggestion given by all
of you and i found below regular expression is working for me.

regsub -all {\s*\n\s*\n(\s*\n)*} $data "\n" data

Thanks a lot,
--Puneet

chaudhar...@gmail.com

unread,
Dec 23, 2016, 2:03:58 AM12/23/16
to
Hi Larry,
In a file, if starting line is blank line . then how can we remove it.

below is not working:
regsub -all {\s*\n\s*\n(\s*\n)*} $data "\n" data


Thanks,
Vipin

Harald Oehlmann

unread,
Dec 23, 2016, 2:47:21 AM12/23/16
to
# Beginning with no \n:
regsub {^\n+} $data "" data
# In the middle and at the end
regsub -all {\n\n+} $data "\n" data

?
0 new messages