1

How can i search for //packages/thirdparty/release/main.txt #10 and replace #10 with #15 (set VARIABLE="#15") using batch commands and also ignoring spaces. #10 can be anything, lets assume #XXX.

testlog.txt

   //packages/thirdparty/release/config.txt                    #8
   //packages/thirdparty/release/config2.txt   #3
   //packages/thirdparty/release/config1.txt    #4
   //packages/thirdparty/release/main.txt            #10

This is my try

@ECHO OFF
set $FindStr=//packages/thirdparty/release/main.txt#10
set $ReplString=//packages/thirdparty/release/main.txt#15

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type testlog.txt') do (
   set $Ver=%%a
   set $Ver=!$Ver: =!
   If /i !$Ver!==%$FindStr% set $Ver=%$ReplString%
   echo !$Ver! ) >> testlog.txt

2 Answers 2

3

I'm not sure what you mean by "ignoring spaces". I assume they are in the original file, so you can't ignore them.

In a Perl one-liner, this keeps whatever spaces were there before the #99 field, and changes it to #15.

perl -pe"s|//packages/thirdparty/release/main.txt\s*\K#\d+|#15|" testlog.txt

The \K ("keep") construct requires version 10 or later of Perl 5. If you have an earlier version and cannot update, then you can use a capture and replace instead, like this

perl -pe"s|(//packages/thirdparty/release/main.txt\s*)#\d+|$1#15|" testlog.txt
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason it didn't work. It seems like something is missing.
\K requires Perl 5.10 or newer.
2

You could use a one line Perl:

perl -pe"s!(//packages/thirdparty/release/main.txt *)#\d+!$1#15!;" testlog.txt 

replaces #10 by #15

3 Comments

thank you Miguel for replying, but #10 can be any number and i would only like to replace" //packages/thirdparty/release/main.txt #XX" with #15
Thanks ikegami and Miguel.
one more quick question, suppose "//packages/thirdparty/release/main.txt" is variable in bash script. How can i use it in above script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.