Page 1 of 1
Eliminating multiple empty lines
Posted: 26 Oct 2019 12:44
by stephjo
Hello DOS pepole,
I have a 1000 text files and would like to replace three (or more) empty lines with two empty lines. How could I do this?
That is, I'd like to replace three (or more) new line characters with two new line characters.
I'm sure there's an easy way in DOS than writing a c++ software. Thank you.
-Steph
Re: Eliminating multiple empty lines
Posted: 26 Oct 2019 13:13
by aGerman
I assume it would be even easier in C++ than in Batch. And probably hybrid scripts like JREPL.BAT would be easier and safer to use than pure Batch solutions. But now that you ask ...
Code: Select all
@echo off &setlocal
set "src=test.txt"
set "dst=test2.txt"
setlocal EnableDelayedExpansion
set /a "empty=0"
<"!src!" >"!dst!" (
for /f %%i in ('type "!src!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
set "line=" &set /p "line="
if not defined line (
set /a "empty+=1"
) else (
if !empty! gtr 1 (echo(&echo() else (for /l %%k in (1 1 !empty!) do echo()
set /a "empty=0"
echo(!line!
)
)
if !empty! gtr 1 (echo(&echo() else (for /l %%k in (1 1 !empty!) do echo()
)
Steffen
Re: Eliminating multiple empty lines
Posted: 26 Oct 2019 15:45
by Eureka!
stephjo wrote: ↑26 Oct 2019 12:44
I have a 1000 text files and would like to replace three (or more) empty lines with two empty lines. How could I do this?
That is, I'd like to replace three (or more) new line characters with two new line characters.
3 empty lines would require 4 "new line characters" ..
BTW (and off-topic): I would use PowerShell for this to avoid problematic characters like % , >, etc.
Something like this:
Code: Select all
$INFILES = "T:\*.txt"
$OUTFOLDER = "T:\test"
gi $INFILES | % {
$A = gc -Raw $_
$A -creplace("(`r`n){4,}","`r`n`r`n`r`n") | Out-File ($OUTFOLDER + "\" + $_.Name) -encoding ascii
}
Subtitled:
get a list of all files $INFILES (T:\*.txt) and for each:
- do a regex replace of 4 or more CRLF's with 3 CRLF's on the content
- write the result to $OUTFOLDER (T:\test\) with UTF8 encoding
(other common encodings: unicode, bigendianunicode, utf8 (this is with BOM))
Re: Eliminating multiple empty lines
Posted: 26 Oct 2019 16:33
by aGerman
Eureka! wrote: ↑26 Oct 2019 15:45
3 empty lines would require 4 "new line characters" ..
Unless at the beginning of the file
Steffen
Re: Eliminating multiple empty lines
Posted: 27 Oct 2019 02:24
by Eureka!
aGerman wrote: ↑26 Oct 2019 16:33
Unless at the beginning of the file
You are right! Never occurred to me that a text file could start with empty lines
In case it does (does it ever?):
Code: Select all
$INFILES = "T:\*.txt"
$OUTFOLDER = "T:\test"
gi $INFILES | % {
( gc -Raw $_ ) -creplace("^(`r`n){3,}","`$1`$1") -creplace("(`r`n){4,}","`r`n`r`n`r`n") | Out-File ($OUTFOLDER + "\" + $_.Name) -encoding ascii
}