Solved

DOS batch managing blank lines (CRLF) using for

Posted on 2015-02-04
3
307 Views
Last Modified: 2015-02-04
I am trying to create a copy of a text file by changing a single specific line that matches a pattern.  I wrote the batch script below which works fine except it creates the output without the blank lines (CRLF) in the original. I tried to explicitly echo blank lines with code like if "x!line!"=="x" echo.>>output.txt to no avail.

for /f "tokens=* delims=" %%s in ('type input.txt ^| find /v "" ') do (
	set line=%%s
	
	if "!line:~0,21!"=="specific_pattern"	(
		echo this_new_line_instead>> output.txt
	) else (
		echo !line!>> output.txt
	)

)

Open in new window


How to preserve the blank lines in the original file?
0
Comment
Question by:TAI-
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40588303
for /f just skips over empty lines, and never enters the loop body.
You can do a workaround by telling find.exe to add the line number, and then strip the line number right off again:
@echo off
setlocal enabledelayedexpansion
set InFile=input.txt
set OutFile=output.txt
for /f "tokens=1* delims=]" %%s in ('type "%InFile%" ^| find.exe /n /v "" ') do (
	set line=%%t
	if "!line:~0,21!"=="specific_pattern"	(
		>>"%OutFile%" echo.this_new_line_instead
	) else (
		>>"%OutFile%" echo.!line!
	)
)

Open in new window

0
 
LVL 7

Expert Comment

by:DrAtomic
ID: 40588327
0
 

Author Closing Comment

by:TAI-
ID: 40588703
Works perfect.

While searching how to do this I actually saw examples of using the /n parameter for find. But as luck would have it they were examples where the line numbers were actually used so it never occured to me to just strip them.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch Login Script - Update default homepage 8 50
Batch File tool box 5 50
Import filename in batch 2 35
Batch/VBScript : Disable Windows tasks 7 34
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

823 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