Find/replace in file from another file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find/replace in file from another file.
# 8  
Old 09-04-2014
Hello KDDubai333,

Could you please try the following code if any user will not present in password file it will leave the field's value as it is.

Code:
awk -vs1="<password-encrypted>" -vs2="</password-encrypted>" 'NR==FNR{a[$1]=$4;next} (v in a){set=1} {{if($0 ~ /<value>/){v=$0;gsub(/value/,X,v);gsub(/^[[:space:]]+/,X,v);gsub(/[<>/]/,X,v);print $0}} {if($0 ~ s1 && set == 1){$0=s1 a[v] s2;set=0}} {if($0 !~ /<value>/) {print $0}}}' FS="$"  passwd_file input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-04-2014 at 02:28 AM.. Reason: spelling correction
# 9  
Old 09-04-2014
Hi SriniShoo,
Executed and observed below problem.

Code:
[cs2050@hostname DS-change]$ awk 'NR == FNR{a[$1] = $2; next} /<value>/ {split($0, t, />|</); v=t[3]} /<password-encrypted>/ && a[v] {sub(/<password-encrypted>.*<\/password-encrypted>/, ("<password-encrypted>" a[v] "</password-encrypted>"))}1' passwd.txt File.xml 
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd">
  <name>user_1_app_DS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@pocof-scan:1520/DEV</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>user_1_app</value>
      </property>
    </properties>
</password-encrypted>ed>{AES}333paUEqje9PJXuoxRreB42dTkejdudHavfDZDhyDUd7AJLkyFfOu0VYxjg47tNz
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>10</initial-capacity>
    <max-capacity>100</max-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    <statement-cache-size>100</statement-cache-size>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/user_1_app_DS</jndi-name>
    <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
  </jdbc-data-source-params>
  <jdbc-xa-params>
    <xa-transaction-timeout>180</xa-transaction-timeout>
  </jdbc-xa-params>
</jdbc-data-source>
[cs2050@hostname DS-change]$

# 10  
Old 09-04-2014
Hi KDDubai333,

Did you try my code in post#8, could you please try the same and let me know if that helps.

Thanks,
R. Singh
# 11  
Old 09-04-2014
Hi Ravinder, password did not change as per the passwd.txt file.

Code:
[cs2050@hostname DS-change]$ cat passwd.txt 
user_1_app	{AES}333paUEqje9PJXuoxRreB42dTkejdudHavfDZDhyDUd7AJLkyFfOu0VYxjg47tNz
user_2_db	{AES}333BmY0PIh/GOy4iqFEr4dXLppsT3cpSU0j9ylIG3YY=
john_3_cat	{AES}M/333+JKrlDsnFdHTsO+A5sEKlkR5IWDdG0IrNobggQ=
boby_5_dd	{AES}333NPEB1Z1B3Qr+0S5j5e7OUJ2Gmz/P1Y+6auN215sY=
MMM_APP	{AES}444NPEB1Z1B3Qr+0S5j5e7OUJ2Gmz/P1Y+6auN215sY=
[cs2050@CUSVLXDB01 DS-change]$ awk -vs1="<password-encrypted>" -vs2="</password-encrypted>" 'NR==FNR{a[$1]=$4;next} (v in a){set=1} {{if($0 ~ /<value>/){v=$0;gsub(/value/,X,v);gsub(/^[[:space:]]+/,X,v);gsub(/[<>/]/,X,v);print $0}} {if($0 ~ s1 && set == 1){$0=s1 a[v] s2;set=0}} {if($0 !~ /<value>/) {print $0}}}' FS="$" passwd.txt File.xml
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd">
  <name>user_1_app_DS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@pocof-scan:1520/DEV</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>user_1_app</value>
      </property>
    </properties>
    <password-encrypted>{AES}y11NPEB1Z1B3Qr+0S5j5e7OUJ2Gmz/P1Y+6auN215sY=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>10</initial-capacity>
    <max-capacity>100</max-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    <statement-cache-size>100</statement-cache-size>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/user_1_app_DS</jndi-name>
    <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
  </jdbc-data-source-params>
  <jdbc-xa-params>
    <xa-transaction-timeout>180</xa-transaction-timeout>
  </jdbc-xa-params>
</jdbc-data-source>

# 12  
Old 09-04-2014
Hello KDDubai333,

It is working perfectly for me as follows.

Input xml file used is as follows:
Code:
cat test_test1
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="Schema Versions for http://xmlns.oracle.com/weblogic/security" xmlns:wls="Schema Versions for http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdb...ata-source.xsd">
  <name>user_1_app_DS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@pocof-scan:1520/DEV</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>user_1_app</value>
      </property>
    </properties>
    <password-encrypted>{AES}y11NPEB1Z1B3Qr+0S5j5e7OUJ2Gmz/P1Y+6auN215sY=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>10</initial-capacity>
    <max-capacity>100</max-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    <statement-cache-size>100</statement-cache-size>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/user_1_app_DS</jndi-name>
    <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
  </jdbc-data-source-params>
  <jdbc-xa-params>
    <xa-transaction-timeout>180</xa-transaction-timeout>
  </jdbc-xa-params>
</jdbc-data-source>


Input passwd file used is as follows:
Code:
cat test_test2
user_1_app$$${AES}8t6paUEqje9PJXuoxRreB42dTkejdudHavfDZDhyDUd7AJLkyFfOu0VYxjg47tNz
user_2_db$$${AES}QKfBmY0PIh/GOy4iqFEr4dXLppsT3cpSU0j9ylIG3YY=
john_3_cat$$${AES}M/Oaw+JKrlDsnFdHTsO+A5sEKlkR5IWDdG0IrNobggQ=
boby_5_dd$$${AES}y11NPEB1Z1B3Qr+0S5j5e7OUJ2Gmz/P1Y+6auN215sY=


Code used is as follows.

Code:
awk -vs1="<password-encrypted>" -vs2="</password-encrypted>" 'NR==FNR{a[$1]=$4;next} (v in a){set=1} {{if($0 ~ /<value>/){v=$0;gsub(/value/,X,v);gsub(/^[[:space:]]+/,X,v);gsub(/[<>/]/,X,v);print $0}} {if($0 ~ s1 && set == 1){$0=s1 a[v] s2;set=0}} {if($0 !~ /<value>/) {print $0}}}' FS="$"  test_test2 test_test1


Output for same is as follows.
Code:
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="Schema Versions for http://xmlns.oracle.com/weblogic/security" xmlns:wls="Schema Versions for http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdb...ata-source.xsd">
  <name>user_1_app_DS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@pocof-scan:1520/DEV</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>user_1_app</value>
      </property>
    </properties>
<password-encrypted>{AES}8t6paUEqje9PJXuoxRreB42dTkejdudHavfDZDhyDUd7AJLkyFfOu0VYxjg47tNz</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>10</initial-capacity>
    <max-capacity>100</max-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    <statement-cache-size>100</statement-cache-size>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/user_1_app_DS</jndi-name>
    <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
  </jdbc-data-source-params>
  <jdbc-xa-params>
    <xa-transaction-timeout>180</xa-transaction-timeout>
  </jdbc-xa-params>
</jdbc-data-source>


kindly let me know if this helps.

Thanks,
R. Singh
# 13  
Old 09-04-2014
in post 11, it was my mistake as i thought you are using latest password file format.
with your latest code. output is same as post 9.
can you please check with latest password file format, if that make it easy? ( post 1 has been edited with the same)

meanwhile i am going to paste what i was trying to do to achieve this.

---------- Post updated at 10:51 AM ---------- Previous update was at 10:45 AM ----------

i am trying like this. but in "if condition" there is problem in matching values.. i should trim extra part (<value> and </value> ) before comparing.

#script
Code:
set -x
keyfile=/home/cs2050/scripts/DS-change/passwd.txt
FILE_IN=/home/cs2050/scripts/DS-change/File.xml
key=`grep value $FILE_IN|head -1`
{ while read line; do
userid=`echo $line| awk '{ print $1 }'`
passwd=`echo $line| awk '{ print $2 }'`
if [[ $key == $userid ]] ; then
sed -i "s/.password-encrypted.*/$passwd/g" FILE_IN
break
fi
done } < $keyfile
cat $FILE_IN

#output with set -x
Code:
+ [[         <value>user_1_app</value> == user_1_app ]]   - not matching


Do you guys think it will work?

Thanks,

Last edited by Scrutinizer; 09-04-2014 at 04:01 AM.. Reason: Additional code tags
# 14  
Old 09-04-2014
Hello KDDubai333,

Please try following we should be good.

Code:
awk -vs1="<password-encrypted>" -vs2="</password-encrypted>" 'NR==FNR{a[$1]=$2;next} (v in a){set=1} {{if($0 ~ /<value>/){v=$0;gsub(/value/,X,v);gsub(/^[[:space:]]+/,X,v);gsub(/[<>/]/,X,v);print $0}} {if($0 ~ s1 && set == 1){$0=s1 a[v] s2;set=0}} {if($0 !~ /<value>/) {print $0}}}'   passwd_file input_xml_file

Output will be as follows.

Code:
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="Schema Versions for http://xmlns.oracle.com/weblogic/security" xmlns:wls="Schema Versions for http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Schema Versions for http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdb...ata-source.xsd">
  <name>user_1_app_DS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@pocof-scan:1520/DEV</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>user_1_app</value>
      </property>
    </properties>
<password-encrypted>{AES}333paUEqje9PJXuoxRreB42dTkejdudHavfDZDhyDUd7AJLkyFfOu0VYxjg47tNz</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>10</initial-capacity>
    <max-capacity>100</max-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    <statement-cache-size>100</statement-cache-size>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/user_1_app_DS</jndi-name>
    <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
  </jdbc-data-source-params>
  <jdbc-xa-params>
    <xa-transaction-timeout>180</xa-transaction-timeout>
  </jdbc-xa-params>
</jdbc-data-source>


Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace in a file from another file

Hello, I am writing a phonetic converter for French from written French to IPA. French has the convention of putting an apostrophe and joining 2 words if the first word ends in an e. l'homme d'air s'est Loading all such words in my dictionary just over loads the database. Apart from this... (6 Replies)
Discussion started by: gimley
6 Replies

2. UNIX for Beginners Questions & Answers

Need to find and replace in a file

Hi All, I am having below sample data in a file. I need to find all the line form this file with word ABC and i need to replace the characters at position 120 which is "CO:BOGFDUI"(30chars) in the lines with blank space. I have tried using grep to find the word with ABC (grep ABC filename),... (3 Replies)
Discussion started by: abhi_123
3 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Find and Replace in File

Legends, I have a file /tmp/list.txt I want to find "/bin/" and replace it with "/log/" I tried the follwoing but no luck Sandy: /tmp> perl -pi -e 's/\/bin\/\/log\/' /tmp/list.txt >> /tmp/try Substitution pattern not terminated at -e line 1. AND, Sandy: /tmp> perl -pi -e... (2 Replies)
Discussion started by: sdosanjh
2 Replies

5. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

6. Shell Programming and Scripting

find and replace a string in a file without the use of temp file

Hi - I am looking for a replacing a string in a in multiple *.sql files in directory with a new string without using a temporary file Normally I can use sed command as below for W in ls `FILE*.sql` do sed 's/OLD/NEW/g' $W > TEMPFILE.dat mv TEMPFILE.dat $W done But Here in my... (9 Replies)
Discussion started by: raghutapal
9 Replies

7. Shell Programming and Scripting

Find and replace in a gz file

Is there a way to do a find and replace in a .gz file in a single script ? I can always unzip, find and replace and then zip it again but would hate to do this everytime. Thanks ! Vivek (1 Reply)
Discussion started by: vashah
1 Replies

8. Shell Programming and Scripting

Find and replace in a file

Hi everyone, I am new to the world of shell script programming. I have a file named Fnd1.txt which has the contents as below. I need to replace the \t with the tab space. Can any one help me to write a perl scipt for this. USA45V1\tG\t341029 USAV1T1\tG\t450545 USAREJ1\tG\t572645... (5 Replies)
Discussion started by: vinay123
5 Replies

9. Shell Programming and Scripting

find and replace pattren in file

Hi, I have the input file having data as follow: file1.txt 001 aaa_1:abcd 002 bbb_2:abcd I want output as, 001xabcd 002xabcd Here iam trying to replace "{1 space}{alphanumeric string with underscore}{:}" with characrter "x". I tried to achieve this using sed;but Iam not getting this... (5 Replies)
Discussion started by: gopalss
5 Replies

10. UNIX for Dummies Questions & Answers

Find replace within a file?

I build several files by using the cut command to grab select fields(columns) from a really bid csv file. Each file is one column of data. I then put them together using paste command. Here is the code built in tcsh: cut -d , -f 1 some.csv > 1.csv cut -d , -f 10 some.csv > 10.csv paste 1.csv... (2 Replies)
Discussion started by: yankee428
2 Replies
Login or Register to Ask a Question