The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing the last data of each line ina file jisha Shell Programming and Scripting 6 08-04-2008 04:47 AM
Replacing a line in a file - HELP!!! maxmave Shell Programming and Scripting 1 06-04-2008 08:55 PM
Replacing end of line with " in a UNIX file The Observer Shell Programming and Scripting 2 05-17-2008 02:20 AM
Replacing characters in file with line break johnemb Shell Programming and Scripting 10 04-26-2007 04:38 AM
replacing first line or lines in a file Terrible UNIX for Advanced & Expert Users 3 06-28-2006 05:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2007
Registered User
 

Join Date: Jul 2007
Posts: 5
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-25-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Code:
sed '/KSG\/Password/{n;s#<value>.*</value>#<value>psoft123</value>;}' abc.xml
Reply With Quote
  #3 (permalink)  
Old 07-25-2007
Registered User
 

Join Date: Jul 2007
Posts: 5
Stumble this Post!
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>;}
Reply With Quote
  #4 (permalink)  
Old 07-25-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,542
Stumble this Post!
if you are open to alternative, here's a Python script:
Code:
#!/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:
Code:
 # ./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>
Reply With Quote
  #5 (permalink)  
Old 07-25-2007
Registered User
 

Join Date: Jul 2007
Posts: 5
Stumble this Post!
I don't know python scripting , so I wont be able to complete my whole script ,
thank you for the reply
Reply With Quote
  #6 (permalink)  
Old 07-25-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Quote:
Originally Posted by malavm View Post
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>;}
sorry - my bad:
Code:
sed '/KSG\/Password/{n;s#<value>.*</value>#<value>psoft123</value>#;}' abc.xml
Reply With Quote
  #7 (permalink)  
Old 07-25-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,572
Stumble this Post!
Code:
awk '{ if ( match($0, "KSG/Password") ) { getline; print "<value>psoft123<\/value>" } else { print } }' filename
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0