Solved

DOS batch managing blank lines (CRLF) using for

Posted on 2015-02-04
3
300 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now