0

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.

  • Your examples show a mixture of one and two spaces between the 01 and the 000... fields. Which is correct? Can either occur? Does the replacement string require the same number of spaces? – AFH May 9 '18 at 22:13
  • 1
    Oh you'd be amazed how versatile text editors are, because they support regular expressions. So , in a text editor like notepad++ try ticking a regex checkbox and doing [0-9]* and you will see it will match that string of digits. But a batch file or any language should be able to do what you want too. – barlop May 9 '18 at 22:24
  • Apologies the example data is in poor formatting on my part. There needs to be 2 spaces between the 01 and the large integer string and one space between the integer string and the XYS marker. – Grant SDC May 10 '18 at 7:30
  • @GrantSDC ok how about @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 – barlop May 11 '18 at 1:32
  • 1
    I also tested a simple example like a file with just the line abc[multiple spaces]def and it treats abc as the first token and def as the second. So it counts either a space or a sequence of contiguous spaces, to be a delimiter. It's worth using simple examples if you want to understand it better. – barlop May 11 '18 at 1:34
1

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

0

One way to parse the file and keep empty lines is to let the lines be numbered through findstr
(so they aren't empty for the for /f loop)
and discard the number afterwards.

:: Q:\Test\2018\05\11\SU_1321271.cmd
@Echo off&SetLocal

Set "ZEROES=00000000000000000000000000000000000"

( for /f "tokens=1,2* delims=: " %%A in (
    'findstr /N "^" RawData.txt'
  ) do If "%%B"=="01" (
      echo=%%B  %ZEROES% XYZ
  ) else (
      echo=%%B  %%C
  )
) >NewData.txt

> type NewData.txt
01 00000000000000000000000000000000000 XYZ

02  157

01 00000000000000000000000000000000000 XYZ

02  158
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.