how to solve insert line with <abc>

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 207
Joined: 31 Jul 2017 09:57

how to solve insert line with <abc>

#1 Post by goodywp » 28 Oct 2021 09:36

Hi all,

I search up that there are some scripts to do the insert job after certain line or certain string. However, if the insert string contain <abcdef> it can not do the insert any more, complain the
The syntax of the command is incorrect.
How we can solve this problem.

For example, I have a xml file sample.xml

I would like to insert one line <serial_number id="serial_number">CT100000PT000271</serial_number> after line 877
I tried the below code

Code: Select all

setlocal DisableDelayedExpansion
set randomline=877
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" sample1.xml') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > sample2.xml
exit /b

:insertblock
echo 						<serial_number id="serial_number">CT100000PT000271</serial_number>
But it complains "The syntax of the command is incorrect."
if you removed < and > from <serial_number id="serial_number">CT100000PT000271</serial_number> it is fine. So how we deal with < and > in the string?
Any thoughts we can do this job?
Thanks
WY

atfon
Posts: 129
Joined: 06 Oct 2017 07:33

Re: how to solve insert line with <abc>

#2 Post by atfon » 28 Oct 2021 10:14

goodywp wrote:
28 Oct 2021 09:36
if you removed < and > from <serial_number id="serial_number">CT100000PT000271</serial_number> it is fine. So how we deal with < and > in the string?
Any thoughts we can do this job?
Thanks
WY
Keep in mind that < and > are re-directional characters. Try to escape them with the ^ character like ^< and ^>.

https://ss64.com/nt/syntax-redirection.html

Squashman
Expert
Posts: 4354
Joined: 23 Dec 2011 13:59

Re: how to solve insert line with <abc>

#3 Post by Squashman » 28 Oct 2021 10:14

Third time today I have answered this problem on different Internet forums. It should be quite obvious to you that the > is used for redirecting standard output. So that is essentially a special character. The same goes for < character. It represents standard input. So you need to escape those characters with the ^ character if you need to use them literally.

Code: Select all

echo 						^<serial_number id="serial_number"^>CT100000PT000271^</serial_number^>

goodywp
Posts: 207
Joined: 31 Jul 2017 09:57

Re: how to solve insert line with <abc>

#4 Post by goodywp » 28 Oct 2021 11:41

Squashman wrote:
28 Oct 2021 10:14
Third time today I have answered this problem on different Internet forums. It should be quite obvious to you that the > is used for redirecting standard output. So that is essentially a special character. The same goes for < character. It represents standard input. So you need to escape those characters with the ^ character if you need to use them literally.

Code: Select all

echo 						^<serial_number id="serial_number"^>CT100000PT000271^</serial_number^>
Thanks a lot!!!!

goodywp
Posts: 207
Joined: 31 Jul 2017 09:57

Re: how to solve insert line with <abc>

#5 Post by goodywp » 12 Nov 2021 12:33

Since the number of that string can be changed from times to times, so we can not hard coded the line as original code. Now I modify the code as below, but not working, what could be wrong

Code: Select all

FOR /F "delims=: tokens=1*" %%i in ('findstr /N /I "acq_device_id" "apex_merchant_1_single_chase.xml"') do ( set lnum=%%i )
echo %lnum%

ren apex_merchant_1_single_chase.xml apex_merchant_1_single_chasea.xml

setlocal DisableDelayedExpansion
set randomline=%lnum%
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" apex_merchant_1_single_chasea.xml') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > apex_merchant_1_single_chase.xml
exit /b

:insertblock
echo 						^<serial_number id="serial_number"^>CT100000PT000271^</serial_number^>
I could find this line number by echo %lnum% but it would not insert the line as expected. What could go wrong here.
Thanks

aGerman
Expert
Posts: 4325
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to solve insert line with <abc>

#6 Post by aGerman » 13 Nov 2021 06:14

goodywp wrote:
12 Nov 2021 12:33

Code: Select all

FOR /F "delims=: tokens=1*" %%i in ('findstr /N /I "acq_device_id" "apex_merchant_1_single_chase.xml"') do ( set lnum=%%i )
echo %lnum%
Try echo "%lnum%" rather than echo %lnum% in the second line. I'm pretty sure you will find a space appended to the line number due to the way you assign it in the first line.

Steffen

goodywp
Posts: 207
Joined: 31 Jul 2017 09:57

Re: how to solve insert line with <abc>

#7 Post by goodywp » 14 Nov 2021 13:22

Thanks Steffen! You are absolutely correct. There is a space because of the first line ( set lnum=%%i ).
Appreciated!

Post Reply