Sponsored Content
Top Forums Shell Programming and Scripting BASH - Compare 2 Files, Output All Matches Post 302907534 by Don Cragun on Sunday 29th of June 2014 04:44:59 PM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 10:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy