Solved

DOS batch managing blank lines (CRLF) using for

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
USB backup batch file help needed. 9 85
What are the big features of MVC5? 4 76
Issue while creating batch file using powershell 4 58
Import filename in batch 2 25
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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

23 Experts available now in Live!

Get 1:1 Help Now