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:

<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

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 :

<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. :slight_smile:

Did you consider

 grep -f file2 file1

?

1 Like

I am getting error message

grep: Invalid range end

Well, then it'd be time to post OS & grep version, as it worked for me exactly as posted.

1 Like

Or try:

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. :wink:

1 Like

It's working. Thanks.

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.

Show us the output from:

ls -l TEXTFILEONE.txt TEXTFILETWO.txt

on your two files.

Which command line? I gave you a suggestion and RudiC gave you a suggestion.

Show us the exact diagnostics that were printed when your process was killed (using CODE tags; not HTML tags).