Replace variable value in first file based on records in second


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace variable value in first file based on records in second
# 1  
Old 07-26-2018
Replace variable value in first file based on records in second

Hello ,

I have below files
a) File A
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0">
    <project name="source">
        <mapping name="m_Source">
            <parameter name="SQL_Query">select  * from $$SCHEMA_NAME."$$TABLE_NAME" where $$COL > $$VALUE and $CONDITIONS</parameter>
            <parameter name="CONDITIONS">Default</parameter>
        </mapping>
    </project>
</root>

b) File b
Code:
$$SCHEMA_NAME=test
$$COL=LOAD_DATETIME
$$TABLE_NAME=table1
$$VALUE=1234

I want to replace the values of $$SCHEMA_NAME , $$COL , $$TABLE_NAME, $$VALUE with the values mentioned in right ( i.e. test,DATETIME,table1,1234) in file a

thanks for your help!!
# 2  
Old 07-26-2018
You may use sed:
Code:
sed -r -i -e 's/$$SCHEMA_NAME/test/' yourfile.xml

if your SCHEMA_NAME is in the shell variable 'schema', you can use this:

Code:
sed -r -i -e 's/$$SCHEMA_NAME/'"$schema/" yourfile.xml

Watch out to see the quotings right!

you may use more than one Parameter -e 's/search/replace/' in one command.

The selection of $$IDENTIFIER is a bit harder to manage since $ is a special character which is used in the shell vor substituting variables. So it must be quoted properly to work. A better choice may be to use %%IDENTIFIER%%.

And regarding this one...

Code:
$$SCHEMA_NAME=test
$$COL=LOAD_DATETIME
$$TABLE_NAME=table1
$$VALUE=1234

If you think this is a correct shell variable assignment: It's not. correctly you would use:

Code:
SCHEMA_NAME=test
COL=LOAD_DATETIME
TABLE_NAME=table1
VALUE=1234

Access of the value of a variable is done via one $ - character:

Code:
echo $SCHEMA_NAME

This User Gave Thanks to stomp For This Post:
# 3  
Old 07-26-2018
If you have bash version 4:
Code:
#!/bin/bash
# bash version 4 required

# associative array
declare -A AR

# read file into array
while IFS="=" read key val
do
  AR[$key]=$val
done < FileB
# now process the main input file, IFS= = keep leading spaces
while IFS= read -r line
do
  # cycle through each key
  for key in "${!AR[@]}"
  do
    val=${AR[$key]}
    # substitute key with value, // = globally = try many times
    line=${line//$key/$val}
  done
  echo "$line"
done < FileA

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-26-2018
How about
Code:
sed -f <(sed 's/^/s=/; s/$/=/' file2) file1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0">
    <project name="source">
        <mapping name="m_Source">
            <parameter name="SQL_Query">select  * from test."table1" where LOAD_DATETIME > 1234 and $CONDITIONS</parameter>
            <parameter name="CONDITIONS">Default</parameter>
        </mapping>
    </project>
 </root>


EDIT: or
Code:
sed 's/^/s=/; s/$/=/' file2 | sed -f- file1


Last edited by RudiC; 07-26-2018 at 11:34 AM..
These 3 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filter records from a log file based on timestamp

Dear Experts, I have a log file that contains a timestamp, I would like to filter record from that file based on timestamp. For example refer below file - cat sample.txt Jan 19 20:51:48 mukul-Vostro-14-3468 systemd: pam_unix(systemd-user:session): session opened for user root by (uid=0)... (6 Replies)
Discussion started by: mukulverma2408
6 Replies

2. UNIX for Beginners Questions & Answers

Replace integer string in a variable based on month?

Hi Folks - Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64 I have a process that determines the start and end load periods for an Oracle data load process. The variables used are as follows follows: They are populated like such: However, the load requires the month to be the... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

3. Shell Programming and Scripting

Filter records based on 2nd file

Hello, I want to filter records of a file if they fall in range associated with a second file. First the chr number (2nd col of 1st file and 1st col of 2nd file) needs to be matched. Then if the 3rd col of the first file falls within any of the ranges specified by the 2nd and 3rd cols , then... (4 Replies)
Discussion started by: ritakadm
4 Replies

4. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

5. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

6. UNIX for Dummies Questions & Answers

Delete records from a big file based on some condition

Hi, To load a big file in a table,I have a make sure that all rows in the file has same number of the columns . So in my file if I am getting any rows which have columns not equal to 6 , I need to delete it . Delimiter is space and columns are optionally enclosed by "". This can be ... (1 Reply)
Discussion started by: hemantraijain
1 Replies

7. UNIX for Dummies Questions & Answers

Filtering records from 1 file based on some manipulation doen on second file

Hi, I am looking for an awk script which should help me to meet the following requirement: File1 has records in following format INF: FAILEd RECORD AB1234 INF: FAILEd RECORD PQ1145 INF: FAILEd RECORD AB3215 INF: FAILEd RECORD AB6114 ............................ (2 Replies)
Discussion started by: mintu41
2 Replies

8. Shell Programming and Scripting

parallel while loop based on the file records

Hi, I need to execute parallel with while loop. Input File(source_file.csv) contains filenames the below source_file.csv file contains Customer1.txt Product1.txt Sales.txt Emp.txt Dept.txt Based on the number of rows that file I want to run the script ‘n' times. while... (2 Replies)
Discussion started by: onesuri
2 Replies

9. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

10. UNIX for Dummies Questions & Answers

Filtering records of a file based on a value of a column

Hi all, I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g. COL1 COL2 COL3 ............... COL47 1 XX 45 ... (4 Replies)
Discussion started by: risk_sly
4 Replies
Login or Register to Ask a Question