China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 02:31
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question about using a batch file to modify file contents [Solved] View 978 Replies 1
Original Poster Posted 2010-09-19 12:18 ·  中国 广东 深圳 电信
新手上路
Credits 2
Posts 2
Joined 2010-09-16 16:33
15-year member
UID 174298
Gender Male
Status Offline
Original structure of file a.ini:
<p1:Item>
<p1:Key>CBP_514_SMDB</p1:Key>
<p1:Type>Database</p1:Type>
<p1:Database>
<p1:Type>SMDB</p1:Type>
<p1:SMDB>
<p1:Host>10.71.110.175</p1:Host>
<p1:Protocol>TELNET</p1:Protocol>
<p1:Port>23</p1:Port>
<p1:UID>smdb13</p1:UID>
<p1:PWD>bGu7HJLDe7ib5QI0WJia2g==</p1:PWD>
<p1:SysomcUID/>
<p1:SysomcPWD>qmrOF5nY34i8yYrXmljISQ==</p1:SysomcPWD>
</p1:SMDB>
</p1:Database>
</p1:Item>

Target structure:
<p1:Item>
<p1:Key>CBP_514_SMDB</p1:Key>
<p1:Type>Database</p1:Type>
<p1:Database>
<p1:Type>SMDB</p1:Type>
<p1:SMDB>
<p1:Host>10.71.110.175</p1:Host>
<p1:Protocol>TELNET</p1:Protocol>
<p1:Port>23</p1:Port>
<p1:Protocol2>FTP</p1:Protocol2>
<p1:Port2>21</p1:Port2>
<p1:UID>smdb13</p1:UID>
<p1:PWD>bGu7HJLDe7ib5QI0WJia2g==</p1:PWD>
<p1:SysomcUID/>
<p1:SysomcPWD>qmrOF5nY34i8yYrXmljISQ==</p1:SysomcPWD>
</p1:SMDB>
</p1:Database>
</p1:Item>

That is, add the following two lines:
<p1:Protocol2>FTP</p1:Protocol2>
<p1:Port2>21</p1:Port2>

Conditions for adding them:
There are many item entries with this kind of structure in a.ini. Only entries whose <p1:Type> is SMDB should get them added. If those two lines already exist in the item itself, then they do not need to be added either.

Many thanks

[ Last edited by w61655 on 2010-9-20 at 09:47 ]
Floor 2 Posted 2010-09-20 09:45 ·  中国 广东 深圳 电信
新手上路
Credits 2
Posts 2
Joined 2010-09-16 16:33
15-year member
UID 174298
Gender Male
Status Offline
Use gawk to do it,

BEGIN{ftp_flag=0;smdb_flag=0}{

if ($0 ~ "<p1:Item>")
{
print $0

getline line
print line

while (line !~ "</p1:Item>")
{
getline line
print line

while (line ~ "<p1:Type>SMDB</p1:Type>")
{
getline line
print line

while (line !~ "</p1:SMDB>")
{
smdb_flag=1

lines=line
getline line
row++

if (line ~ "<p1:Protocol2>FTP</p1:Protocol2>" || line ~ "<p1:Protocol2>SFTP</p1:Protocol2>")
{
ftp_flag=1
}
}

if(smdb_flag=1)
{
for(i=1;i<row;i++)
{
print lines
if(lines ~ "<p1:Port>" && ftp_flag==0)
{
print "<p1:Protocol2>FTP</p1:Protocol2>"
print "<p1:Port2>21</p1:Port2>"
}
}
print line

ftp_flag=0
smdb_flag=0
delete lines
row=0
}
}
}
next
}
print $0
}
Forum Jump: