How to replace a same value fpr different variables with different values?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a same value fpr different variables with different values?
# 1  
Old 05-22-2012
Java How to replace a same value fpr different variables with different values?

Hi,

Could anyone help me on the below problem Smilie,

I am having a .xml file as a input file with >1000 lines
In that i want to replace some values,
ex:
Code:
<name>username</name>
  <value>admin</value>
  <name>password</name>
  <value>admin</value>

.
.
.

now i want to replace the 'username' value as 'admin123' instead of 'admin' but the 'password' value must be 'admin'.

here i used sed command to replace the value, but it replacing the both values.

Please can anyone help me out from this...

Last edited by Scott; 05-22-2012 at 04:47 AM.. Reason: Code tags, please...
# 2  
Old 05-22-2012
Code:
$ sed "/username/{N;s/admin/admin123/g;}" file
<name>username</name>
<value>admin123</value>
<name>password</name>
<value>admin</value>

# 3  
Old 05-22-2012
Thanks Scott Smilie

Its working fine.
but in the same file i am having name variable as
Code:
<name>/JDBC/userName</name>
<value>admin</value>
<name>/JDBC/password</name>
<value>admin</value
<name>/JNDI/username</name>
<value>admin</value>
<name>/JNDI/password</name>
<value>admin</value>

I tried with the above command which you mentioned
but its displaying error.
Code:
sed "/JDBC/userName/{N;s/admin/admin123/g;}" input.xml

could you please help me out from this..

Last edited by Franklin52; 05-22-2012 at 07:45 AM.. Reason: Please use code tags
# 4  
Old 05-22-2012
What error would that be?

Code:
$ sed "/[uU][sS][eE]...[mM][eE]/{N;s/admin/admin123/g;}" file
<name>/JDBC/userName</name>
<value>admin123</value>
<name>/JDBC/password</name>
<value>admin</value
<name>/JNDI/username</name>
<value>admin123</value>
<name>/JNDI/password</name>
<value>admin</value>

# 5  
Old 05-22-2012
Scott,

Code:
 sed: -e expression #1, char 7: unknown command: `u'

The above error has been displayed when i executed the below command

Code:
 sed "/JDBC/userName/{N;s/admin/admin123/g;}" input.xml

the last command whic you gave is working for username
but for password?
What i mean that if '/' are present in between the words which we are searching then the above command is not working.

Please advise on this?

Last edited by Scott; 05-22-2012 at 08:08 AM.. Reason: Code tags
# 6  
Old 05-22-2012
If you want to include a / in the pattern either escape it (\/), or use a different separator:

Code:
sed "@/JDBC/userName@.....@" ....

Changing the password is not fundamentally different from changing the username.

And, please:
Moderator's Comments:
Mod Comment Use Code Tags
# 7  
Old 05-22-2012
And now i want to change it to only one value
that means
If i want to change it to JDBC/password value then it doesnt effect to the JNDI/password value.

---------- Post updated at 06:15 AM ---------- Previous update was at 06:09 AM ----------

I tried with that
but it thrown error

Code:
$ sed "@JNDI/password@{N;s/admin/4dm1n/g;}" input.xml
sed: -e expression #1, char 1: unknown command: `@'


Last edited by Scott; 05-22-2012 at 08:20 AM.. Reason: 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

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

4. Shell Programming and Scripting

Exporting Values of Variables to Another File

Hello, I'm very new to Linux, and I have a question, I'm hoping you could help me with. :) I have created a file called subject, contains this code: #!/bin/bash read -p "Student Name: " NAME read -p "Student ID: " ID read -p "Address: " ADDRESS I'm to create another file called... (7 Replies)
Discussion started by: iwant2learn
7 Replies

5. Shell Programming and Scripting

How to use values in variables

Hi Legends, Please help me in solving the below: I have the file size and file name using `ls` and awk command. ls -ltr *.pf |awk '{print $5, $9}' 2000003072 ABC.pf 2000003072 DEF.pf 2000003072 GHI.pf 56000588 JKL.pf 2000003072 MNO.pf 2000003072 PQR.pf Now, I want to take the... (3 Replies)
Discussion started by: sdosanjh
3 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. Shell Programming and Scripting

Getting values from a block into variables

Hi there, If I run "ipmitool bmc info" on any of my x86 boxes, i get Device ID : 32 Device Revision : 1 Firmware Revision : 1.1 IPMI Version : 2.0 Manufacturer ID : 42 Manufacturer Name : Sun Microsystems Product ID ... (7 Replies)
Discussion started by: rethink
7 Replies

8. Shell Programming and Scripting

how to concatenate values of two variables with an underscore(_) in between

Hi, I'm new to shell programming. I have two variables a and b a=val1 b=val2 could anyone kindly post the shell script to concatenate the values of variable a and b with an underscore(_) in between? The final output should be val1_val2. (8 Replies)
Discussion started by: badrimohanty
8 Replies

9. Shell Programming and Scripting

read values from ps -ef into variables in ksh?

Hi, I want to get the first two items returned by ps -ef into two variables? Can anyone please help Thanks (8 Replies)
Discussion started by: JamesByars
8 Replies

10. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies
Login or Register to Ask a Question