Extract sentence and its details from a text file based on another file of sentences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract sentence and its details from a text file based on another file of sentences
# 1  
Old 09-08-2015
Extract sentence and its details from a text file based on another file of sentences

Hi

I have two text files. The first file is TEXTFILEONE.txt as given below:


Code:
<Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text>
<Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323" Created="2011-03-16T17:01:02+0000" use_count="234">This is the second text</Text>
<Text Text_ID="10155645315851111_10155645320006543" From="1481727095384343" Created="2011-03-16T17:02:04+0000" use_count="3456">This is the third text 
If counted  
GOT IT... ����</Text>
<Text Text_ID="10155645315851111_10155645326223345" From="411021195696329" Created="2011-04-16T17:03:44+0000" use_count="5433">This is is just a sample text.......</Text>
<Text Text_ID="10155645315851111_10155645326223445" From="411021195692389" Created="2011-04-16T17:03:44+0000" use_count="5433">This is just another sample text........</Text>
<Text Text_ID="10155645315851111_10155645326222345" From="411021195696789" Created="2011-04-16T17:03:44+0000" use_count="5433">This is the fourth text........</Text>

The second file is TEXTFILETWO.txt

Code:
This is the first text
This is the second text
This is the third text
If counted
GOT IT... ����
This is the fourth text........

I need help to extract all the sentence and its details from TEXTFILEONE.txt which matches all the sentences from the file TEXTFILETWO.txt.


The sample output will be :

Code:
<Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text>
<Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323" Created="2011-03-16T17:01:02+0000" use_count="234">This is the second text</Text>
<Text Text_ID="10155645315851111_10155645320006543" From="1481727095384343" Created="2011-03-16T17:02:04+0000" use_count="3456">This is the third text 
If counted  
GOT IT... ����</Text>
<Text Text_ID="10155645315851111_10155645326222345" From="411021195696789" Created="2011-04-16T17:03:44+0000" use_count="5433">This is the fourth text........</Text>


Thanks in advance. Smilie

Last edited by Don Cragun; 10-18-2015 at 06:40 PM.. Reason: Change HTML tags to CODE tags.
# 2  
Old 09-08-2015
Did you consider
Code:
 grep -f file2 file1

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-08-2015
Error

I am getting error message

Code:
grep: Invalid range end


Last edited by Don Cragun; 10-18-2015 at 06:40 PM.. Reason: Change HTML tags to CODE tags.
# 4  
Old 09-08-2015
Well, then it'd be time to post OS & grep version, as it worked for me exactly as posted.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-08-2015
Or try:
Code:
grep -Ff file2 file1

But, as RudiC said, it is ALWAYS a good idea to tell us what OS and shell you're using when you post questions here. Smilie
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-08-2015
It's working. Thanks.
# 7  
Old 10-18-2015
I am trying to use this command line for two large files. After entering the command line, the process got Killed. What should I do? I am using Ubuntu 14.04.2 LTS and bash shell. It worked for small size files.

Last edited by my_Perl; 10-18-2015 at 05:54 PM.. Reason: editing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract jil file details in a excelsheet

I am very new to shell scripting. I have a autosys jil file that looks like :-- /* ------------- JOB1 ------------------ */ insert_job: JOB1 job_type: b owner: cm@pelonmuck permission: gx,ge,wx,we,mx,me date_conditions: 1 days_of_week: mo,tu,we,th,fr,su start_time: "18:30"... (9 Replies)
Discussion started by: newbie_shell
9 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Extract sequences from a FASTA file based on another file

I have two files. File1 is shown below. >153L:B|PDBID|CHAIN|SEQUENCE RTDCYGNVNRIDTTGASCKTAKPEGLSYCGVSASKKIAERDLQAMDRYKTIIKKVGEKLCVEPAVIAGIISRESHAGKVL KNGWGDRGNGFGLMQVDKRSHKPQGTWNGEVHITQGTTILINFIKTIQKKFPSWTKDQQLKGGISAYNAGAGNVRSYARM DIGTTHDDYANDVVARAQYYKQHGY >16VP:A|PDBID|CHAIN|SEQUENCE... (7 Replies)
Discussion started by: nelsonfrans
7 Replies

4. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

5. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

Extract details from XML file

Hi , I have one xml file contains more than 60 lines. I need to extract some details from the file and store it in new file.Not the whole file Please find the xml file below: <?xml version="1.0" encoding="UTF-8"?> <DeploymentDescriptors xmlns="http://www.tibco.com/xmlns/dd"> ... (6 Replies)
Discussion started by: ckchelladurai
6 Replies

8. Programming

How to extract a sentences of word from a text file.

Hi , i have a text file that contain a story How do i extract the out all the sentences that contain the word Mon. in C++ I only want to show those sentences that contain the word mon eg. Monkey on a tree. Rabbit jumping around the tree. I am very rich, I have lots of money. Today... (1 Reply)
Discussion started by: xiaojesus
1 Replies

9. Shell Programming and Scripting

extract from a file based on values in another file

Hello, I have two files that have delimited entries as shown below. I would like to use either Perl or Shell script to extract all the rows in File 1 corresponding to values in File 2 and output it to another File. File 1 ------- 1 36 24 Object1 2 45 36 Object2 3 96 ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

10. Shell Programming and Scripting

i want to extract details for particular file

Hi i have following file uuid ( RO) : 62701790-60da-dd9a-669d-a563aac1c435 host-uuid ( RO): 5f3f668d-a7c7-4e5f-a4a6-6e90fafb50ed sr-uuid ( RO): 62103d07-e0aa-acf3-2d9f-414ad3377bd0 device-config (MRO): location: /dev/xapi/block ... (6 Replies)
Discussion started by: bp_vardhaman
6 Replies
Login or Register to Ask a Question