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
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
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
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 |
|______________________________________________________________________|
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/ >
How about
regsub -all {(\s*\n)+} $data "\n" data
HTH
Helmut Giese
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?
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.
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
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