Eliminating multiple empty lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stephjo
Posts: 12
Joined: 03 Feb 2014 02:39

Eliminating multiple empty lines

#1 Post by stephjo » 26 Oct 2019 12:44

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Eliminating multiple empty lines

#2 Post by aGerman » 26 Oct 2019 13:13

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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Eliminating multiple empty lines

#3 Post by Eureka! » 26 Oct 2019 15:45

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))

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Eliminating multiple empty lines

#4 Post by aGerman » 26 Oct 2019 16:33

Eureka! wrote:
26 Oct 2019 15:45
3 empty lines would require 4 "new line characters" ..
Unless at the beginning of the file :wink:

Steffen

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Eliminating multiple empty lines

#5 Post by Eureka! » 27 Oct 2019 02:24

aGerman wrote:
26 Oct 2019 16:33
Unless at the beginning of the file :wink:
You are right! Never occurred to me that a text file could start with empty lines :o


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
}

Post Reply