Grep, count and match two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep, count and match two files
# 1  
Old 05-26-2006
Grep, count and match two files

I am writing the below script to do a grep and count number of occurances between two tab delimited files.

I am trying to achieve..

1) Extract column 2 and column 3 from the S.txt file. Put it in a temp pattern file
2) Grep and count column 2 in D.txt file
3) Compare the counts between D.txt and S.txt files.
4) Abort if counts doesn't match

Example: APPLE occurs 4 times in D.txt and is a match in S.txt

Code:
#!/usr/bin/ksh

SUM_COUNT=`nawk '{if ($0 ~ /^S/) print $2,$3 >"S1.txt" }'` S.txt

for i in S1.txt
do
DETAIL_COUNT=`grep $i D.txt | wc -l`
if [ ${DETAIL_COUNT} -eq ${SUM_COUNT} ]
then
     echo "Count between Detail and Summary matches"
else
     echo "Count didn't match"
      exit
fi
done

The script goes in a loop and never exits....I am not sure if this is the right way to code.

S.txt
PHP Code:
S       APPLES                          4
S       ORANGES                         1
S       PEARS                           1
S       PINEAPPLES                      1
S       TOMATOES                        0
S       PEPPERS                         1 
D.txt
PHP Code:
D       PINEAPPLES
D       ORANGES
D       PEARS
D       APPLES
D       APPLES
D       APPLES
D       APPLES
D       PEPPERS 
I am still in the learning phase and would appreciate any input on this..
# 2  
Old 05-26-2006
Gosh I hope this isn't homework related but this mish mash of commands was too much for me. It's way too easy to do this kind of stuff right in awk by itself. Take a look at this quick example (just so happens I just helped a coworker with a nearly identical business related problem on very large flat files):

Code:
nawk '
    # Compile summary array
    FILENAME=="S.txt" {
        SKeys[$2]=$3
    }

    # Compile details array
    FILENAME=="D.txt" {
        DKeys[$2]++
    }

    END {
        # Look for summary records that do not match detail counts
        for (i in SKeys) {
            if (SKeys[i] != DKeys[i]) {
                # Add a record to the merged set
                MKeys[i]++
            }
        }
        # Look for detail counts where a summary record is missing
        for (i in DKeys) {
            if (SKeys[i] != DKeys[i]) {
                # Add a record to the merged set
                MKeys[i]++
            }
        }

        # Print a merged set of records
        printf("%-20s %10s %10s\n", "Fruit", "Summary", "Detail")
        printf("%-20s %10s %10s\n", "====================", "==========", "==========")

        for (i in MKeys) printf("%-20s %10d %10d\n", i, SKeys[i], DKeys[i])
    }
' S.txt D.txt

results:
Code:
Fruit                   Summary     Detail
==================== ========== ==========
BACON                         0          8
APPLES                        4          6

S.txt
Code:
S       APPLES                          4 
S       ORANGES                         1 
S       PEARS                           1 
S       PINEAPPLES                      1 
S       TOMATOES                        0 
S       PEPPERS                         1

D.txt
Code:
D       PINEAPPLES 
D       ORANGES 
D       PEARS 
D       APPLES 
D       APPLES 
D       APPLES 
D       BACON
D       BACON
D       BACON
D       APPLES 
D       APPLES 
D       APPLES 
D       PEPPERS 
D       BACON
D       BACON
D       BACON
D       BACON
D       BACON

# 3  
Old 05-30-2006
Thank You Thomas....It is absolutely not a homework problem. It is an ACR validation check for a bank. Actually we have a authorization file which has partner names in the Detail text file and the Summary file with partner names and the count of records for each partner name.

We get this file every week and we need to validate the count of records for each partner name. (The keys in your code).

The Detail file is almost 15 million records and the Summary file has about 4 or 5 records.

I will take your example and test it out on these huge files tomorrow...
# 4  
Old 05-30-2006
try this one,
much simpler one :::

Code:
#! /usr/bin/ksh

index=1
tmp=1

for val in `sed 's/^.*  //' d.txt`
do
  if [ $tmp -eq 1 ]
  then
     fruit[$index]=$val
     cnt[$index]=0
     tmp=$(($tmp + 1))
  fi
  temp=1
  while [ $temp -le $index ]
  do
     if [ ${fruit[$temp]} = $val ]
     then
        cnt[$temp]=$((${cnt[$temp]} + 1))
        break
     fi
     temp=$(($temp + 1))
  done
if [ $temp -gt $index ]
  then
     index=$(($index + 1))
     fruit[$index]=$val
     cnt[$index]=$((${cnt[$index]} + 1))
  fi
done
awk '{print $2, $3}' s.txt | while read first second
do
temp=1
while [ $temp -le $index ]
do
  if [ ${fruit[$temp]} = $first ]
  then
     if [ cnt[$temp] -eq $second ]
     then
        print ${fruit[$temp]} ${cnt[$temp]}
        break
     else
        exit 1
     fi
  fi
  temp=$(($temp + 1))
done
if [ $temp -gt $index ]
then
exit 1
fi
done

exit 0

# 5  
Old 05-30-2006
Quote:
Originally Posted by madhunk
I am trying to achieve..

1) Extract column 2 and column 3 from the S.txt file. Put it in a temp pattern file
2) Grep and count column 2 in D.txt file
3) Compare the counts between D.txt and S.txt files.
4) Abort if counts doesn't match
Try
Code:
while read kk FRUIT NUMBER
do
if [ $(grep -c $FRUIT D.txt) -ne $NUMBER ]
then
   echo " $FRUIT Does not match."
   exit
fi
done<S.txt


Last edited by Klashxx; 05-30-2006 at 05:35 AM..
# 6  
Old 05-30-2006
Quote:
4) Abort if counts doesn't match
the above point is not met.

unnecessary entire file parsing each time.
this could have been avoided
# 7  
Old 05-30-2006
Thank you very much for the help..

I tested Thomas script and it produced no output somehow....It also didn't abort...

I tried Madan's script with the actual file and it produced an output like this
parse.ksh[22]: no space

I need to compare the count of records for each partner name from the Detail file (which is the authoritative source) with the count number from the Summary file.

If the Detail file has records like..

PHP Code:
D       MIDWEST FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       MIDWEST FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       CAESAR    FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       AIRTRAN FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       AIRTRAN FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       AIRTRAN FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       CAESAR  FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       MIDWEST FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON
D       MIDWEST FIRSTNAME    LASTNAME              209 N SANBORN AVE               JEFFERSON 
Summary File

PHP Code:
S       MIDWEST                         4
S       ORBITZ                          0
S       AIRTRAN                         3
S       FRONTIER                        0
S       CAESAR                          2 
Detail file should be compared with the Summary file...For example, MIDWEST has 4 records in the Detail file and the Summary file has MIDWEST with count 4, then it is a PASS else FAIL.

I am not sure at this point, which one should I be following...If you can please help me on this, that would be really appreciated.

Thank You,
Madhu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count the number of files to delete doesnt match

Good evening, need your help please Need to delete certain files before octobre 1 2016, so need to know how many files im going to delete, for instance ls -lrt file_20160*.lis!wc -l but using grep -c to another file called bplist which contains the list of all files backed up doesn match... (7 Replies)
Discussion started by: alexcol
7 Replies

2. UNIX for Dummies Questions & Answers

Grep Files with and without match

Hi There, How do i finding files with match and without match Normally, I will use grep -l 'Hello' grep -L 'Hello World' How do we combined (2 Replies)
Discussion started by: alvinoo
2 Replies

3. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

5. Shell Programming and Scripting

Pattern match using grep between two files

Hello Everyone , I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file cat test1.txt vikas vikasjain j ain testt douknow hello@vik@ # 33 ||@@ vcpzxcmvhvdsh... (1 Reply)
Discussion started by: mailvkjain
1 Replies

6. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

7. UNIX for Dummies Questions & Answers

Grep bunch of gzip files to count based on category

Started using unix commands recently. I have 50 gzip files. I want to grep each of these files for a line count based particular category in column 3. How can I do that? For example Sr.No Date City Description Code Address 1 06/09 NY living here 0909 10st st nyc 2 ... (5 Replies)
Discussion started by: jinxx
5 Replies

8. Shell Programming and Scripting

Grep string from logs of last 1 hour on files of 2 different servers and calculate count

Hi, I am trying to grep a particular string from the files of 2 different servers without copying and calculate the total count of its occurence on both files. File structure is same on both servers and for reference as follows: 27-Aug-2010... (4 Replies)
Discussion started by: poweroflinux
4 Replies

9. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

10. UNIX for Advanced & Expert Users

grep count across multiple files

I have a number of simulation log files and I want to get a total count of the "PASSED" expression in them. If I use grep -c <files>, grep would give a tally for each file. I just want one number, the total count. How do I do that? (4 Replies)
Discussion started by: CrunchMunch
4 Replies
Login or Register to Ask a Question