Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two .rtf file.... The first one have this content:

Apple, Orange, Banana, Noodle, Chip

The Second File is something like this:

     Apple I love eat Apple.
     Banana I hate Banana.
     Zoo I want to go Zoo.
     Noodle Noodle can be a very very very very very very very very very very very long, but still is one line.
     Chip Don't eat so many chip. 
     Orange Orange is great, not Apple plx. Noodle
     Water Drinking water is boring.

The first file is a "key" of second file. In the second file, the first word is the key of each line. Each key and sentence in second file ONLY have one line. The Second File have many lines with key, but not all the key is shown on file1, but file1's key MUST in the second file. How can I get the result like this: (Need to sort by the key from File1)

  Apple, Apple I love eat Apple. 
  Orange, Orange is great, not Apple plx.
  Banana, I hate Banana. 
  Noodle, can be a very very very very very very very very very very very long, but still is one sentence. 
  Chip, Don't eat so many chip.
share|improve this question
up vote 1 down vote accepted

I will make some assumptions (which you seem to agree with in your question).

  1. Key file is a CSV of keys (comma separated list of key words)
  2. Data file has keys as the first words starting at the first column
    • this constraint can be managed with some more things in the script
  3. Data file has no-two-lines which start with the same key word
    • if this constraint is broken, you will get all the matching lines
      when you look for the key.
    • that can be handled with a "| tail -1" to show just the first match (say)
  4. You start saying rtf files but tag the question as text.
    If you have rich-text-format files, you should convert them to text files for this purpose.

here is a script for you,

#    -> to look for comma separated words in key.txt

for k in $(sed 's| ||' key.txt)
#          -----------> to make the search easier
  grep "^$k " data.txt
#       ----> look for key words matching at the start of each line

Here key.txt is your first file and data.txt is your second file.
The for loop sorts the output in the order of the keys.
The sed command removes any spaces you may have in the key.txt file to make searching easier.

Update on non-english characters (which is why it seems you say RTF file):
Look for iconv and get your RTF converted to UTF-8 -- I think grep can handle that.
If that is what you want to do your question should be rephrased as,

"How to grep in rich-text-formatted files (on linux)?"

share|improve this answer

If you didn't need the result sorted in the order of the keyfile:

sed 's/, /\n/g' keyfile.txt | grep -f - datafile.txt

In order to do the sorting, you could do something like this (in Bash):

sed 's/, /\n/g' keyfile.txt | grep -f - datafile.txt | sort | join -1 2 <(sed 's/, /\n/g' keyfile.txt | nl | sort -k2) - | sort -k2 | cut -d' ' -f1,3-
share|improve this answer

This Perl script will do that:

use strict;
use warnings;

open (my $f1, '<', $ARGV[0]) || die "cannot open $ARGV[0] $!\n";
open (my $f2, '<', $ARGV[1]) || die "cannot open $ARGV[1] $!\n";

my $line=join('',<$f1>);
my @f2=<$f2>;

foreach my $e1 (sort split /, /,$line) {
    foreach my $e2 (@f2) {
        print "$e1, $e2" if ($e2=~/^$e1/);
share|improve this answer

Another option assuming your data file is 'data.txt':

for k in Apple Orange Banana Noodle Chip; do echo -n "$k, "; grep "^$k" data.txt; done
share|improve this answer
some question on txt, can the txt display some non-English char/word? – user28167 May 6 '10 at 14:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.