Solved

DOS batch managing blank lines (CRLF) using for

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch script to move disabled user accounts to different OU 5 98
file size 9 53
batch file problem 5 67
Command prompt output to variable? 9 59
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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