replacing a line of unknown charecters in a file

Hi All

I have a requirement where using a script I grep a file for string (KSG/Password in below ) , get the next line which is the password and I need replace the whole line of unknown special charecters (encrypted password) with another line as given below .

As in below i need to get the line , assign it to a variable
STRNG="<value>#!OE/+puLDjXm/Yg4W34vHvV6rdiGx0plE</value>" then replace

$STRNG with "<value>psoft123</value>"

The file i grep contains as below

<value>
</NameValuePair>
<NameValuePairPassword>
<name>KSG/Password</name>
<value>#!OE/+puLDjXm/Yg4W34vLvV6rdiGx0plE</value>
</NameValuePairPassword>
<NameValuePair>
<name>KSG/DB_User</name>
<value>vass</value>
</NameValuePair>

when I use 'sed' as below , I get the error "sed:command garbled"

sed 's/'$STRNG'/'<value>psoft123</value>'/' abc.xml > abc1.xml

+ sed s/ <value>#!OE/+puLDjXm/Yg4W34vHvV6rdiGx0plE</value>/<value>psoft123</value>/ abc.xml
+ 1> abc1.xml
sed: command garbled: s/

Can some one please help me with this

Thanks
Malavm

sed '/KSG\/Password/{n;s#<value>.*</value>#<value>psoft123</value>;}' abc.xml

Hi

Thanks for the reply but I am still getting the error

bash-2.05$ sed '/KSG\/Password/{n;s#<value>.</value>#<value>psoft123</value>;}' abc.xml
sed: command garbled: /KSG\/Password/{n;s#<value>.
</value>#<value>psoft123</value>;}

if you are open to alternative, here's a Python script:

#!/usr/bin/python
data=open("file").readlines()
data=[i.strip() for i in data]
for num,line in enumerate(data):
    if "KSG/Password" in line:
        data[num+1] = "<value>psoft123</value>"

for i in data:
 print i

output:

 # ./test.py #or use > to redirect to new file
<value>
</NameValuePair>
<NameValuePairPassword>
<name>KSG/Password</name>
<value>psoft123</value>
</NameValuePairPassword>
<NameValuePair>
<name>KSG/DB_User</name>
<value>vass</value>
</NameValuePair>

I don't know python scripting , so I wont be able to complete my whole script ,
thank you for the reply

sorry - my bad:

sed '/KSG\/Password/{n;s#<value>.*</value>#<value>psoft123</value>#;}' abc.xml
awk '{ if ( match($0, "KSG/Password") ) { getline; print "<value>psoft123<\/value>" } else { print } }' filename

vgersh99

Your code is working fine , thanks a lot for the solution. :slight_smile:
I am trying to understand the code , I am not able to figure out , can you help me please.

matrixmadhan

when I execute the commond , I am getting the error as below ,

bash-2.05$ awk '{ if ( match($0, "KSG/Password") ) { getline; print "<value>psoft123<\/value>" } else { print } }' abc.xml
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

thanks

Surprised ! :confused::confused:

Its perfectly working for me.

By any change did you miss out any " ' " or " } " ?

( I don't think so you would have )

Could you please check that again ? :slight_smile:

Perhaps nawk vs awk issue ?

Hi

I pasted the command and the result I ran on the shell in my before post ,
I am using bash on solaris 5.9 does it matter ?? ,

Thanks

I don't think so that would be the reason ! :slight_smile:

( Possibly :rolleyes::rolleyes: )

I don't have access to solaris 5.9 box right now.

May be somebody might execute and clarify the doubt ! :slight_smile: