Finding contents of one file in another file

48,872

Solution 1

grep itself is able to do so. Simply use the flag -f:

grep -f <patterns> <file>

<patterns> is a file containing one pattern in each line; and <file> is the file in which you want to search things.

Note that, to force grep to consider each line a pattern, even if the contents of each line look like a regular expression, you should use the flag -F, --fixed-strings.

grep -F -f <patterns> <file>

If your file is a CSV, as you said, you may do:

grep -f <(tr ',' '\n' < data.csv) <file>

As an example, consider the file "a.txt", with the following lines:

alpha
0891234
beta

Now, the file "b.txt", with the lines:

Alpha
0808080
0891234
bEtA

The output of the following command is:

grep -f "a.txt" "b.txt"
0891234

You don't need at all to for-loop here; grep itself offers this feature.


Now using your file names:

#!/bin/bash
patterns="/home/nimish/contents.txt"
search="/home/nimish/another_file.csv"
grep -f <(tr ',' '\n' < "${patterns}") "${search}"

You may change ',' to the separator you have in your file.

Solution 2

Another solution:

  • use awk and create your own hash(e.g. ahash), all controlled by yourself.
  • replace $0 to $i and you can match any fields you want.

awk -F"," '
{  
   if (nowfile==""){ nowfile = FILENAME;  }

   if(FILENAME == nowfile)
   {
     hash[$0]=$0;
   }
   else
   {
       if($0 ~ hash[$0])
       {  
           print $0
       }
   }
} '  xx yy

Solution 3

I don't think you really need a script to perform what you're trying to do.

One command is enough. In my case, I needed an identification number in column 11 in a CSV file (with ";" as separator):

grep -f <(awk -F";" '{print $11}' FILE_TO_EXTRACT_PATTERNS_FROM.csv) TARGET_FILE.csv

Share:
48,872
dna
Author by

dna

Updated on March 27, 2020

Comments

  • dna
    dna about 4 years

    I'm using the following shell script to find the contents of one file into another:

    #!/bin/ksh
    file="/home/nimish/contents.txt"
    
    while read -r line; do
        grep $line /home/nimish/another_file.csv
    done < "$file"
    

    I'm executing the script, but it is not displaying the contents from the CSV file. My contents.txt file contains number such as "08915673" or "123223" which are present in the CSV file as well. Is there anything wrong with what I do?

  • dna
    dna about 11 years
    so with grep -f it gives error as ".rep: 0652-033 Cannot open:"
  • Rubens
    Rubens about 11 years
    Well, the problem there seems to be with the file path. Are you sure the path is correct? Try to embrace the file name with double quotes. How is it you're trying to execute. Please, add the command line you're trying to run in the next comment.
  • dna
    dna about 11 years
    so I am just running the script by ./script.ksh
  • Rubens
    Rubens about 11 years
    @NIMISHDESHPANDE Please, try the script I've posted.
  • dna
    dna about 11 years
    thanks ruben... I tried the above script that u mentioned but its giving me error " ( is not expected"
  • Rubens
    Rubens about 11 years
    @NIMISHDESHPANDE I've tried it here and it works fine. Try to do it simply using the command line, replacing the variables with the file paths.
  • dna
    dna about 11 years
    @Ruben.... Buddy its still giving me same error for simple command line as well... the thing is if I simply put grep 0891234 data.csv it does display the record, but does not work in while loop
  • Rubens
    Rubens about 11 years
    @NIMISHDESHPANDE Please, check the example I've posted. If your file with patterns does not have one pattern in each line, post the format of your file, so that we may help you converting it into the proper format.
  • Peter Mortensen
    Peter Mortensen about 4 years
    What is the "xx yy" at the end for?
  • Fırat Uyulur
    Fırat Uyulur over 3 years
    those are the two input files you are searching one in another