BASH - Compare 2 Files, Output All Matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Compare 2 Files, Output All Matches
# 1  
Old 06-29-2014
BASH - Compare 2 Files, Output All Matches

This is probably rehash but I did look. Smilie

I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines in File one have been processed.

Now, check this out, sample data.

File1 - single column of hostnames, using the short name
vsie1p990
vsie1p991
vsie1p992
...

File2 - multi-column, comma separated, the first column is the hostname(shortname)

format: shortname, IP Address, fqdn
> vsie1p992,191.167.44.212,vsie1p992.srv.us.company.com

I tried the following, but something is just not quite right:
Code:
     #!/bin/bash
    echo "Report Generated"
    date
     count=0
     while read list ; do
    {
      IT=`grep -i "$list" $2`
      If [ -n "$IT" ] ; then
         echo "Match Found: $list"
         count=`expr "$count" + 1`
      fi
     }
     done <$1
     echo "Total Matches = $count"

Example run: > ./checkit.sh list1 list2


Any help, advice, guidance would be greatly appreciated.

-Richard
# 2  
Old 06-29-2014
In what way is it just not quite right?

Will a given hostname ever appear on more than one line in list2?

Can a hostname in list1 ever be a substring of another hostname in list1 or list2?

Does the output have to be in the order of hostnames in list1? Is it ok if the output is in the order of appearance of where those hostnames appear in list2?
# 3  
Old 06-29-2014
Hey Don, so if I have a match, I want to echo out the matching item in File2, example

Code:
File1/Item1 = donsServer

File2/Item 5444 = donsServer,192.168.1.44,donesServer.svr.us.domain.com

how would you echo out the match from File2?

I.E.,

Code:
Match Found = donsServer,192.168.1.44,donsServer.svr.us.domain.com

* for each match found

One other thing that is confusing is, IT seems blank, but I know it is not
if I add:
Code:
      while read list ; do
      { 
         IT=`grep -i "$list" $2`
         echo "IT = $IT"

it's blank, ugh...

Thanks for your time
Moderator's Comments:
Mod Comment Please use CODE tags for all sample input, output, and code.

Last edited by Don Cragun; 06-29-2014 at 02:53 AM.. Reason: Add CODE tags.
# 4  
Old 06-29-2014
Assuming that hostnames in file1 are distinct and none of them are a subset of any other hostname, the command:
Code:
grep -Fi -f file1 file2

will give you all lines in file2 that contain any of the hostnames listed in file1 using a case insensitive match.

So, in your code:
Code:
      while read list ; do
      { 
         IT=`grep -i "$list" $2`
         echo "IT = $IT"

where is the closing } and where is the closing done? If you're creating a subshell, variables defined in the subshell won't be available in the parent.
# 5  
Old 06-29-2014
Ok, so here is the deal, I have 179 entries in file1 and I have 118875 entries in file2.

If I do this:

Code:
cat file2 | grep -i <entry from file1>

, it returns a match
Code:
server,ip address, fqdn

The point is, all 179 entries "do" exist in file2, so I simply (ugh :-)) want
to export the hostname,ipaddress,fqdn for each match from file2

Here is the complete code (not working yet):

Code:
#!/bin/bash

clear

echo "Report Generated:" >> outfile
date >> outfile
echo >> outfile

count=0

while read list ; do
{ 
IT=`grep -i "$list" $2`
echo "IT = $IT >> outfile #this value is blank, don't understand why
count=`expr "$count" + 1`
echo "Processing Transaction Number: $count"; >> outfile
echo "$list" >> outfile
if [ -n "$IT" ] ; then
echo "Match Found: $list and $2" >> outfile
fi
}
done <$1
echo "Total Matches: $count" >> outfile

The irony is, code similar to this has worked fine for me in the past with (or using) other data sets. Obviously my "lack of understanding" is now exhibited because the code is not behaving as I would expect it would. Thus my confusion.

Last edited by Scrutinizer; 06-29-2014 at 12:53 PM.. Reason: CODE tags
# 6  
Old 06-29-2014
Quote:
Originally Posted by rcbarr2014
Ok, so here is the deal, I have 179 entries in file1 and I have 118875 entries in file2.

If I do this:

Code:
cat file2 | grep -i <entry from file1>

, it returns a match
Code:
server,ip address, fqdn

The point is, all 179 entries "do" exist in file2, so I simply (ugh :-)) want
to export the hostname,ipaddress,fqdn for each match from file2

Here is the complete code (not working yet):

Code:
#!/bin/bash

clear

echo "Report Generated:" >> outfile
date >> outfile
echo >> outfile

count=0

while read list ; do
{ 
IT=`grep -i "$list" $2`
echo "IT = $IT >> outfile #this value is blank, don't understand why
count=`expr "$count" + 1`
echo "Processing Transaction Number: $count"; >> outfile
echo "$list" >> outfile
if [ -n "$IT" ] ; then
echo "Match Found: $list and $2" >> outfile
fi
}
done <$1
echo "Total Matches: $count" >> outfile

The irony is, code similar to this has worked fine for me in the past with (or using) other data sets. Obviously my "lack of understanding" is now exhibited because the code is not behaving as I would expect it would. Thus my confusion.
I'm surprised that bash ran this script without complaining about the missing " in the line:
Code:
echo "IT = $IT >> outfile #this value is blank, don't understand why

Your code would be easier to read if you would use indentation to make the structure obvious.

The braces ({ and }) in your code are unnecessary, but shouldn't affect the behavior of this script.

You could simplify some of your code by redirecting the output of the while loop instead of redirecting the output of each command in the loop, but that shouldn't affect the output produced by this script either.

What is the name of your script? What are the permissions on your script? How did you invoke your script?

If every line in file1 matches a line in file2 when using the command:
Code:
grep -i <entry_from_file1> file2

and you add the missing quote in:
Code:
echo "IT = $IT" >> outfile #this value is blank, don't understand why

and your script is executable, and you invoke it with something like the command:
Code:
./YourScriptName file1 file2

it seems like you should get something in outfile as long as file1 and file2 do not contain any whitespace characters. (If they do contain whitespace characters and you quoted them appropriately when you invoked your script, your script is still missing quotes around uses of "$1" and "$2" (but, again, if that is the problem, bash should have given you syntax errors).

What does "this value is blank" mean? Is there no output in outfile from the echo? Is there output from each echo, but it is just IT = ? What is in outfile when your script completes?

Please explain how this script is behaving differently than you expect.

Please explain what you expect this script to do.

Please show us what the script is doing?

Please answer the questions I asked in post #2 in this thread.

Does the grep command I suggested in post #4 in this thread produce any output? If so, is it suitable for the main body of your report?
# 7  
Old 06-30-2014
How did you create your files? Using a windows text editor? Then no surprise there's no matches due to the trailing CR char (excerpt from a script run with -vx set):
Code:
grep -i $'vsie1p990\r' file2

You can find out with e.g.
Code:
cat -vet file1
vsie1p990^M$
vsie1p991^M$
vsie1p992^M$

, or hexdump / od.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to output matches and mismatches between two files to one file

In the tab-delimited files, I am trying to match $1,$2,$3,$4,$5 in fiel1 with $1,$2,$3,$4,$5 in fiel2 and create and output file that lists what matches and what was not found (or doesn't match). However the awk below seems to skip the first line and does not produce the desired output. I think... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Using awk to output matches between two files to one file and mismatches to two others

I am trying to output the matches between $1 of file1 to $3 of file2 into a new file match. I am also wanting to output the mismatches between those same 2 files and fields to two separate new files called missing from file1 and missing from file2. The input files are tab-delimited, but the... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

5. Shell Programming and Scripting

Compare two files and get output

Hi, I have two files, file1 and file2 and I need to compare them by line (exact match, order of the lines is not important) and get output with lines from file2 that are not found in file1 (not other way around). How do I do that? With grep or otherwise.. Thankyou (2 Replies)
Discussion started by: orp56
2 Replies

6. Shell Programming and Scripting

Using Bash scripting to compare arrays looking for matches

I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences. Thanks in advance! Karl (9 Replies)
Discussion started by: karlp
9 Replies

7. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

8. Shell Programming and Scripting

compare 2 files > output new to third

Hi, I have a question of comparing to files and output the result third file where file1 is the mainfile containing processed dir data and 2nd file grepīs dirīs data again (could be newer dirs comparing file1<file2) now i wanna make shure that output in file3 only contains newer dirs hx... (1 Reply)
Discussion started by: needle
1 Replies

9. Shell Programming and Scripting

Compare 2 files and send output to other

Hi, In File1.txt I have text like: 23AA3424DD23|3423ff25sd5345| and so on In File2.txt I have similar text as File1, but with ",": 23aa3424dd23,192.168.1.100, and so on I wan to remove the pipes from File1 and select 5 fields, then remove "," from File2.txt and select 2 fields (IP's... (14 Replies)
Discussion started by: cameleon
14 Replies

10. Shell Programming and Scripting

Compare entries in two files, output one of them in bash

I have two files which are of the following format: File1: Unnamed Unnamed Boston Unnamed New_York Unnamed File2: San_Francisco Chicago Portland Austin Orlando Los_Angeles In the case where an entry in File1 is "Unnamed", I want to output the name from the same line in File2.... (5 Replies)
Discussion started by: forthekill
5 Replies
Login or Register to Ask a Question