Compare 2 array files using grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare 2 array files using grep
# 1  
Old 04-22-2009
Data Compare 2 array files using grep

Using the bash shell I am trying to read in 2 files. The first file (file1) is a list of names to search for in (file2). If a name in file1 is found in file2 I want the entire line in file2 to be printed to an output file. File1 consists of a single column of names. File2 consists of several columns of data.

I have been having problems:
1) Reading a File1 into an array
2) Searching for each element in the array in File2
3) Printing the line in File2 on which the element from File1 was found

Here is the script I have so far:

#!/bin/bash

echo "Please enter the file of names to be searched for"

read file1

echo "Please enter the lookup table file"

read file2

for g in $file1
do
grep $g file2
done

Any help would be greatly appreciated.

Thanks, Lanna
# 2  
Old 04-22-2009
Many/most? greps have a -f switch for accepting patterns from a file. If your grep has -f, then just do:
Code:
grep -f file1 file2

No array or for loop and stuff needed then.
# 3  
Old 04-22-2009
Power grep not behaving the way I'd like

Thanks for the reply. That did make things easier however it's not behaving exactly as I'd like.

grep -f file1 file2

This finds every occurrence of the line in file1 in file2. So, I would expect the output to have the same number of lines as file1. However, the output has the same number of lines as file2. Why is it matching a blank line pattern in file1 to lines in file2?

To make his more clear, there are 46 lines in file1 and 51 lines in file2. I expect the output to have 46 lines, one line for every match from file1 found in file2. Instead, the output has 51 lines.

What is going wrong?

Thanks, Lanna
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to use if command to compare in an array?

Hi all, Currently i am facing issue using if command to compare to array value. can someone help me? if }" -gt "${ZDATE_1}" ] then do echo "red" else echo "green" fi done i have checked array is giving values fine but there seems some issue with if command. (2 Replies)
Discussion started by: amyt1234
2 Replies

2. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

compare two array

Hi firnds, I am having two arrays eg. @a=qw(1 2 3 4) and @b=qw(1 3 6 9) Now, I want to compare two arrays on first index of both arrays and if they matched, it should output remaining indexes Pseudo-code if ($a == $b) { print "remaining indexes";} How can i do this using perl? (1 Reply)
Discussion started by: Renesh
1 Replies

4. Shell Programming and Scripting

Compare intervals (columns) from two files (awk, grep, Perl?)

Hi dear users, I need to compare numeric columns in two files. These files have the following structure. K.txt (4 columns) A001 chr21 9805831 9846011 A002 chr21 9806202 9846263 A003 chr21 9887188 9988593 A003 chr21 9887188 ... (2 Replies)
Discussion started by: jcvivar
2 Replies

5. Shell Programming and Scripting

awk, associative array, compare files

i have a file like this < '393200103052';'H3G';'20081204' < '393200103059';'TIM';'20110111' < '393200103061';'TIM';'20060206' < '393200103064';'OPI';'20110623' > '393200103052';'HKG';'20081204' > '393200103056';'TIM';'20110111' > '393200103088';'TIM';'20060206' Now i have to generate a file... (9 Replies)
Discussion started by: shruthi123
9 Replies

6. Shell Programming and Scripting

compare two value in array - Perl

Hi, I just wondering if I want to compare previous value to next value if it same count + 1 if not doesn't count how do we compare in Perl? Thank (2 Replies)
Discussion started by: guidely
2 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Compare Array results

Hi, In a kshell , i need to compare the results of two array . each Non-match value should copy to a new array. For example: ========== Array one contain the following values: A B C Array two contain the following values: C F A E After comparing this arrays , a new array should... (4 Replies)
Discussion started by: yoavbe
4 Replies

9. UNIX for Dummies Questions & Answers

Compare array values

# tail myprocesses.txt 178 processes at Tue Oct 21 14:33:01 IST 2008 16 MySQL processes at Tue Oct 21 14:33:01 IST 2008 175 processes at Tue Oct 21 14:36:01 IST 2008 60 MySQL processes at Tue Oct 21 14:36:01 IST 2008 192 processes at Tue Oct 21 14:39:01 IST 2008 64 MySQL processes at Tue Oct... (2 Replies)
Discussion started by: shantanuo
2 Replies

10. Shell Programming and Scripting

compare a variable against array elements

hi everyone - i have a bash script that does, in broad terms, the following: given a file containing a list of email accounts, for every account, do , then move on to the next account. pretty simple, and all that stuff works fine. thing is, there's a very good change that any account... (2 Replies)
Discussion started by: fearboy
2 Replies
Login or Register to Ask a Question