match text from two files and write to a third file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match text from two files and write to a third file
# 1  
Old 02-02-2010
match text from two files and write to a third file

Hi all

I have two files X.txt and Y.txt. Both file contains same number of sentences. The content of X.txt is

Code:
The filter described above may be combined.

and the content of Y.txt is

Code:
The filter describ+ed above may be combin+ed.

Some of the words are separated with "+" symbol.
Some of the words are equal and some not.

The expected output is :

Code:
The|The|empty filter|filter|empty described|describ+ed above|above|empty may|may|empty be|be|empty combined|combin+ed.

For any matching word in the corresponding location of the corresponding sentences, the same word is copied and suffixed with the word "empty" with a "|" separation. But for unmatched word, print the words from X and Y separated by "|".

I want to write a Perl script. Any help is appreciated.
Thanks in advance.

Last edited by my_Perl; 02-02-2010 at 05:15 AM..
# 2  
Old 02-02-2010
By Awk
Code:
$ awk 'NR==FNR {for (i=1;i<=NF;i++) a[NR FS i]=$i} 
          NR>FNR {for (i=1;i<=NF;i++) {(a[FNR FS i]==$i)?t="|empty":t="" ; printf "%s|%s%s ",a[FNR FS i],$i,t}} 
                      {print ""}' X.txt Y.txt

The|The|empty filter|filter|empty described|describ+ed above|above|empty may|may|empty be|be|empty combined.|combin+ed.

# 3  
Old 02-02-2010
Thanks rdcwayx. It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Match text from file 1 to file 2 and return specific text

I hope this makes sense and is possible. I am trying to match $1 of panel_genes.txt with $3 of RefSeqGene.txt and when a match is found the value in $6 of RefSeqGene.txt Example: ACTA2 is $1 of panel_genes.txt ACTA2 NM_001613.2 ACTA2 NM_001141945.1 awk 'FNR==NR {... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Text match in two files

Trying to match the text from file1 to file2 and print what matches in a new file (match.txt) and what does not in another (missing.txt). awk -F'|' 'NR==FNR{c++;next};c > 0' flugent.txt IDT.txt > match.txt Thank you :). (8 Replies)
Discussion started by: cmccabe
8 Replies

5. Shell Programming and Scripting

Read text between regexps and write into files based on a field in the text

Hi, I have a huge file that has data something like shown below: huge_file.txt start regexp Name=Name1 Title=Analyst Address=Address1 Department=Finance end regexp some text some text start regexp Name=Name2 Title=Controller Address=Address2 Department=Finance end regexp (7 Replies)
Discussion started by: r3d3
7 Replies

6. Shell Programming and Scripting

[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2). Mapping for pattern match and add line : for Alpha 123 for Beta 234 for Gamma 345 no match (goes into another file) File 1 ... (3 Replies)
Discussion started by: w020637
3 Replies

7. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

9. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

10. UNIX for Dummies Questions & Answers

Replace text in match files

Hi all, I want to replace text 'DEF' for those files containing text 'ABC'. I can only locate the files containing text 'ABC', but I don't know how to replace the text 'ABC' with 'DEF'. I use the following command to locate the files containing 'ABC' find . -exec grep -l 'ABC'... (1 Reply)
Discussion started by: wilsonchan1000
1 Replies
Login or Register to Ask a Question