?
Solved

DOS batch managing blank lines (CRLF) using for

Posted on 2015-02-04
3
Medium Priority
?
345 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-
[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
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

801 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