Page 1 of 1

how to solve insert line with <abc>

Posted: 28 Oct 2021 09:36
by goodywp
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

Re: how to solve insert line with <abc>

Posted: 28 Oct 2021 10:14
by atfon
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

Re: how to solve insert line with <abc>

Posted: 28 Oct 2021 10:14
by Squashman
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^>

Re: how to solve insert line with <abc>

Posted: 28 Oct 2021 11:41
by goodywp
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!!!!

Re: how to solve insert line with <abc>

Posted: 12 Nov 2021 12:33
by goodywp
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

Re: how to solve insert line with <abc>

Posted: 13 Nov 2021 06:14
by aGerman
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

Re: how to solve insert line with <abc>

Posted: 14 Nov 2021 13:22
by goodywp
Thanks Steffen! You are absolutely correct. There is a space because of the first line ( set lnum=%%i ).
Appreciated!