Find Key and replace value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Key and replace value
# 1  
Old 11-10-2015
Find Key and replace value

Hi,

I am new to shell scripting. I have a config file where key and value is stored as below. In my shell script, I want to look for Test ID in the config file and replace the value 1 with another value stored in a variable. How would I do that ?

Code:
<Config Key="Test ID" Value="1"/>

I would appreciate anybody's help on this.

Regards,
Vishvar

Last edited by Don Cragun; 11-10-2015 at 06:54 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-10-2015
Code:
sed 's/"Test ID" Value="1"/"Test ID" Value="2"/'

or:
Code:
sed 's/\("Test ID" Value=\)"1"/\1"2"/'


Code:
var="2"
sed "s/\"Test ID\" Value=\"1\"/\"Test ID\" Value=\"$var\"/"

or:
Code:
var="2"
sed "s/\(\"Test ID\" Value=\)\"1\"/\1\"$var\"/"


Last edited by Aia; 11-10-2015 at 11:26 AM..
# 3  
Old 11-10-2015
Hi Aia,

Thanks for reply. When I try to run after adding below line script just hangs, I am not getting even any error message. Is there any prerequisite to use sed?

Code:
sed "s/\"Test ID\" Value=\"1\"/\"Test ID\" Value=\"$var\"/"


Moreover the existing entry in the config file can be any of below such values. It is not always Value="1". So I can't replace as it is. I need to look for key "Test ID" and replace the corresponding value with my desired value whatever the old value is.


Code:
<Config Key="Test ID" Value="1"/>

or
Code:
<Config Key="Test ID" Value="1111"/>

or
Code:
<Config Key="Test ID" Value="abcdef"/>

Regards,
Vishnu

Last edited by Don Cragun; 11-10-2015 at 06:55 PM.. Reason: Add CODE and ICODE tags.
# 4  
Old 11-10-2015
What's the name of the file?
Please, try the following to accommodate your new divulged information. The @ is nothing more that custom delimiters for sed, instead of the default /, which might make it easier to differentiate from all those escapes
Code:
sed "s@\(Test ID\" Value=\"\).*\(\"\/>\)@\1$var\2@" Vishnuvardhanh.file

or perhaps this:
Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" Vishnuvardhanh.file


Last edited by Aia; 11-10-2015 at 01:41 PM..
# 5  
Old 11-10-2015
Hi Aia,

Below line helps, it prints the required result as output but does not actually update the config file.

Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml


Code:
var ="requiredval"

Output prints as below :
Code:
<Config Key="Test ID" Value="requiredval"/>

But still config.xml has old entry as below :
Code:
<Config Key="Test ID" Value="1"/>

---------- Post updated at 01:42 PM ---------- Previous update was at 01:26 PM ----------

Hi Aia,
Thanks a lot for your help.

Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml > tmp.xml && mv tmp.xml ais_config.xml

I moved updates to a temporary file and then moved to original config file, so now it replaced. You can always let me know if any easier way in performance wise.

Regards,
Vishvar

Last edited by Don Cragun; 11-10-2015 at 06:57 PM.. Reason: Add CODE and ICODE tags.
# 6  
Old 11-10-2015
Quote:
Originally Posted by Vishnuvardhanh
Hi Aia,

Below line helps, it prints the required result as output but does not actually update the config file.

sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml


var ="requiredval"
Output prints as below :
<Config Key="Test ID" Value="requiredval"/>

But still config.xml has old entry as below :
<Config Key="Test ID" Value="1"/>
Note: the highlighted red is to bring your attention that variables in the shell can not have spaces between the = symbol

Yes, sed would output the result to the STDOUT (standard output), normally your monitor or screen; it does not modify the file:
if you use the GNU sed this will change the file in place. Make sure the result is what you want.
Code:
sed -i "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml

Or you can redirect the output to a temporary file and then rename it to config.xml
Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml > temp.xml && mv temp.xml config.xml

---------- Post updated at 12:07 PM ---------- Previous update was at 11:49 AM ----------

Quote:
Originally Posted by Vishnuvardhanh
[...]

sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml > tmp.xml && mv tmp.xml ais_config.xml

I moved updates to a temporary file and then moved to original config file, so now it replaced. You can always let me know if any easier way in performance wise.
[...]
There is no need to create a tmp file and rename it if the output file is going to be different that the input file config.xml

Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml > ais_config.xml

That's the only "performance wise" modification I could tell you.
# 7  
Old 11-10-2015
Code:
sed -i "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml

This line creates an empty config.xml file though.

I am using second one and works fine.
Code:
sed "s@\(Test ID\" Value=\"\)[^\"]*\(\"\/>\)@\1$var\2@" config.xml > temp.xml && mv temp.xml config.xml

Thank you Aia ! Smilie

Last edited by Don Cragun; 11-10-2015 at 06:58 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

2. HP-UX

To find where sftp public key is installed for user

Hi All, I am facing an issue wherein a new sft public key for a user is not working on our HP-UX m/c. So i have to check where the sftp public keys for some other sample users were installed earlier in the system i.e. under which folder. Can you please help me out on this. (2 Replies)
Discussion started by: ammbhhar
2 Replies

3. Shell Programming and Scripting

Find and replace value using a key from other file

Dear Friends, I am looking for a way how to find and replace a value in two files using a reference a file where are the key to replace. Basically, I want to keep a copy of the original file and make a new one in order to compare at the end that the change was done whitout change the rest of... (26 Replies)
Discussion started by: jiam912
26 Replies

4. Shell Programming and Scripting

how to find the shortest line which containing a key string?

hi all, suppose a key string: M0271857 and to find all lines containing this key string in a text file which returns multiple lines but i only want the shortest one is there a way to do that? thanks so much! (4 Replies)
Discussion started by: sunnydanniel
4 Replies

5. Linux

It is possible to find out when a particular encryption key was imported in linux

Hi All, In linux server some encryption keys were imported using gpg command. I want to know when those keys was imported. Is there any way to get when the encryption keys were imported? Thanks in advance.. :rolleyes: (1 Reply)
Discussion started by: latika
1 Replies

6. UNIX for Advanced & Expert Users

command to replace the entire line from the key point

Hi everyone I am new to Unix. I got stuck up by small issue. I have text file something like this abc 'xyz' '5' lmn 'pqr' '7' i want to replace the abc 'xyz' '5' to abc 'xyz' '6' but i have a key as 'xyz' based on this key i want to do that. I am not aware of how to use sed... (7 Replies)
Discussion started by: Vijayaragavan
7 Replies

7. Shell Programming and Scripting

grep to find ssh key

I have my ~/.ssh/id_dsa file which contains my public ssh key. I need to determine if this key is found in the authorized_keys file. How would I go about grepping this. This was my original idea: line=`cat ~/.ssh/id_dsa` grep "${line?}" authorized_keys This didn't work because it... (1 Reply)
Discussion started by: Bob565656
1 Replies

8. Shell Programming and Scripting

How to find entering ENTER key?.

Hello All, i have a script to get input from the user like bellow, read -p "Do you want to continue (y/n) : " status i want to identify the pressing of Enter Key with out giving any value for the above statement and i want get the status if we press Enter key during run time. How to... (0 Replies)
Discussion started by: tsaravanan
0 Replies

9. UNIX for Advanced & Expert Users

replace key function

in my system , the ctrl-C is not work ( ctrl-C should be used to break the running process ) , but ctrl-Z works , I don't know why ctrl-C is not work , I still can't find the reason . Now I would like to replace its function --> if the user press ctrl-Z then it will send the same command as ctrl-C... (1 Reply)
Discussion started by: ust
1 Replies

10. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies
Login or Register to Ask a Question