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
Eliminating multiple empty lines
Moderator: DosItHelp
Re: Eliminating multiple empty lines
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 ...
Steffen
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()
)
Re: Eliminating multiple empty lines
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
}
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
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
}