Search & replace fields from file1 to file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search & replace fields from file1 to file2
# 1  
Old 03-02-2011
MySQL Search & replace fields from file1 to file2

hi,

I have two xml files with the name source.xml and tobe_replaced.xml.

Sample data:
source.xml contains:
Code:
<?xml version="1.0"?>
<product description="prod1" product_info="some/info">
<product description="prod2" product_info="xyz/allinfo">
<product description="abc/partialinfo" product_info="abc/partialinfo/edited">
..

tobe_replaced.xml contains:
Code:
<?xml version="1.0"?>
<product description="prod1" product_info="pqr/msg1">
<product description="prod2" product_info="zab/allmsg">
<product description="abc/partialinfo" product_info="abc/partialinfo/unchanged">
..

i want to search description and take the value after =" ie prod1,prod2,abc/partialinfo and also get the product_info values ie some/info,abc/partialinfo/edited .
this is done as follows or similar to this:

Code:
awk -F\" '/description/ { print $2 $4}' source.xml

In tobe_replaced.xml, search the value of $2 and replace product_info with value of $4 for all the lines from source.xml to tobe_replaced.xml.

desired output in tobe_replaced.xml is:
Code:
<?xml version="1.0"?>
<product description="prod1" product_info="some/info">
<product description="prod2" product_info="xyz/allinfo">
<product description="abc/partialinfo" product_info="abc/partialinfo/edited">
..

How do i do this while reading all the lines from both the files?

Last edited by dragon.1431; 03-02-2011 at 06:07 PM.. Reason: corrected text info
# 2  
Old 03-02-2011
try:
Code:
awk 'NR==FNR{a[$2]=$3;next}{$3=a[$2];print $0}' source tobe

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 03-02-2011
Match lines with description and only replace data values in 2nd item (This is closer to OPs original script):

Code:
awk -F\" 'NR==FNR&&/description/{a[$2]=$4;next}/description/{$4=a[$2]}NR!=FNR' OFS=\" source.xml tobe_replaced.xml

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

search from file1 and replace into file2

I have 2 files: file1.txt: 1|15|XXXXXX||9630716||0096000||30/04/2012|E|O|X||||20120525135617-30.04.2012|PAT66OLM|STA||||00001|STA_0096000_YYYPPPXTMEX00_20120525135617_02_P.pdf|... (2 Replies)
Discussion started by: pparthiv
2 Replies

6. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies

7. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

8. Shell Programming and Scripting

Read each word from File1 and search each file in file2

file1: has all words to be searched. 100007 200999 299997 File2: has all file names to be searched. C:\search1.txt C:\search2.txt C:\search3.txt C:\search4.txt Outfile: should have all found lines. Logic: Read each word in file1 and search each file in the list of File2; if the... (8 Replies)
Discussion started by: clem2610
8 Replies

9. Shell Programming and Scripting

Search values between ranges in File1 within File2

Hi people, I have 2 files, one with a list of non consecutive ranges (File1.txt), where each range begins with the value in column 1 and finishes with the value in column 2 in the same line, as can be seen above. 215312581156279 215312581166279 215312582342558 215312582357758... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question