Batch file to replace row containing a specific string value

batchcommand line

I have an interface file for example named RawData.txt which contains different regions of data which can range from a few thousand lines to nearer 100,000 lines of data.

There is a mix of lines which contain for example

01 00000000000000000000000000000198699 XYZ

which have int values other than 0 included that at times need to be replaced with

01 00000000000000000000000000000000000 XYZ

but due to the variablility of the int value between the 01 and XYZ markers a straight forward find and replace in a text editor will not work.

The part I need to manipulate is structured like below:

01  00000000000000000000000000000198699 XYZ

02  157

01  00000000000000000000000000000007749 XYZ

02  158

01  00000000000000000000000000000183279 XYZ

02  163

01  00000000000000000000000000000007749 XYZ

02  165

01  00000000000000000000000000000000000 XYZ

02  175

Ideally I am looking to put together a batch file that searches for any lines in the .txt file starting with the 01 record marker and replaces the line with:

01 00000000000000000000000000000000000 XYZ

I assume the most straight forward approach is to find any lines starting with 01 record marker and replace the whole line as the length needs to be identical after the amendments are made to the existing data.

Most examples I have found related to replacing one string with another as in the example here

https://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra/23076141?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

and

https://stackoverflow.com/questions/16614101/batch-script-find-string-in-text-file-by-line-then-replace-whole-line-with-anot?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

but cannot modify to accomplish what I need from a batch file.

Any advice is appreciated.

Best Answer

This might be what you want

A batch file dealt with this surprisingly neatly!

Where a.a is your data file

blahblah.bat is a one line batch file

C:\Users\harvey>type blahblah.bat
@for /f "tokens=1,2 delims= " %%f in (a.a) do @IF "%%f"=="01" (echo 01 00000000000000000000000000000000000 XYZ) ELSE IF NOT "%%f"=="01" ECHO %%f %%g

C:\Users\harvey>

execute the batch file

C:\Users\harvey>blahblah
01 00000000000000000000000000000000000 XYZ
02 157
01 00000000000000000000000000000000000 XYZ
02 158
01 00000000000000000000000000000000000 XYZ
02 163
01 00000000000000000000000000000000000 XYZ
02 165
01 00000000000000000000000000000000000 XYZ
02 175

C:\Users\harvey>

That did seem to remove blank lines, there may be a way around that with batch, but another way, besides batch, is to use sed.

C:\Users\harvey>sed -r "s/^01.*/01 00000000000000000000000000000000000 XYZ/" a.a

01 00000000000000000000000000000000000 XYZ

02  157

01 00000000000000000000000000000000000 XYZ

02  158

01 00000000000000000000000000000000000 XYZ

02  163

01 00000000000000000000000000000000000 XYZ

02  165

01 00000000000000000000000000000000000 XYZ

02  175

You could of course add >b.b to redirect that output to a new file, so you could do sed -r "......." a.a > b.b i.e. sed -r "s/^01.*/01 00000000000000000000000000000000000 XYZ/" a.a > b.b

Related Question