Solved

remove duplicate line breaks from a txt file using batch file

Posted on 2014-09-30
4
859 Views
Last Modified: 2014-10-02
Hi

My PHP error log files are renamed on a daily basis but they are also full of duplicate carriage returns which I'm currently removing using regex in textpad replacing \n\n with \n

This is my batch file that renames them each day at midnight, how can I incorporate something in it to remove the extra line breaks before saving please?

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "php-errors.log" %%d-%%e-%%f.txt

Open in new window

0
Comment
Question by:Neil Thompson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40351657
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 200 total points
ID: 40351671
Based on the solution on the link http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_23027665.html

here is the code which does requested.

@echo off
set source=C:\30092014
set dest=C:\30092014
for /f "tokens=*" %%a in ('dir /b /a-d "%source%\php-errors.log"') do call :process %%a
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "tmp_php-errors.log" %%d-%%e-%%f.txt

goto :eof
 
:process
set filename=%*
echo Working on %filename%
type "%source%\%filename%" > "%source%\tmp_%filename%"
findstr /B /I /R "[!-z]" "%source%\tmp_%filename%" > "%dest%\%filename%"

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 300 total points
ID: 40351912
Using findstr is a neat trick found by Steve (dragon-it), but we can simplify the above code as we are certain there is only one file - no loop needed here.
@echo off
type php-errors.log | findstr /B /I /R "[!-z]" > %date:~0,2%-%date:~3,2%-%date:~6,4%.txt

Open in new window

or, if you prefer the original date string construction
@echo off
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do set newname=%%d-%%e-%%f.txt
type php-errors.log | findstr /B /I /R "[!-z]" > %newname%.txt

Open in new window

0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 40356808
Many thanks All,
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question