compare grep?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers compare grep?
# 1  
Old 06-15-2005
compare grep?

is there any way to compare two grep commands in an if statement? like:

if grep blah = grep blah blah
then

is there a way to store the values of the greps and compare them? the greps are counting pattern matches in two different files and i need to make sure they match.
# 2  
Old 06-15-2005
why don't you just diff the fiiles? see "man diff" ....
# 3  
Old 06-15-2005
i guess i wasn't specific enough, the compare is a little complicated. In one file i am counting the instances of the pattern with grep -c and i want to compare that count to the value in field 3 of another file where field 1 = C

so, let me try to explain a little easier, i want to compare the values of these two statements:

grep -c <pattern> <file>
grep "^C" <file> | awk -F'|' '{print $3}'

if there is an easier way to do this i'm all ears, i don't even know if this is possible to begin with so any input is appreciated
# 4  
Old 06-15-2005
sounds simple enough ... just to confirm, can you post a line from the second file?
# 5  
Old 06-15-2005
C| |00000009| |00000000|

so in other words, i'm counting in one file the number of time s pattern comes up and i want to see if that number matches the number in field 3, in this case it's 9, do I have to account for the leading zeros in the field when comparing?
# 6  
Old 06-15-2005
not required ... treat both of them like numbers ....

Code:
#! /bin/ksh

InstCount=$(grep -c pattern file1)
ReqdCount=$(awk -F"|" '/^C/ {print $3}' file2)

if [ $InstCount -eq $ReqdCount ]
then
    echo "Counts are the same."
else
    echo "Somebody fibbed on the count."
fi

exit 0

# 7  
Old 06-15-2005
you can do this ( in bash )

c=`grep foo fil1`
d=`grep bar fil2`

if [ "$c" == "$d" ]; then
...
else
...
fi

HTH
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

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

3. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

6. UNIX for Dummies Questions & Answers

Using grep to compare JPEGs

Hi, I'm not sure if this is the right place but I'm a newb so it seemed right. I'm trying to write some code to organize JPEGs. Its a long story, but essentially, I need the program to tell me whether a particular jpeg is of 1) a sheet of paper (lets say its solid pink that takes up the entire... (3 Replies)
Discussion started by: Bengel
3 Replies

7. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the log file each line starts with timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file... (1 Reply)
Discussion started by: achu
1 Replies

8. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the logfile , lines contains timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file format.Please... (1 Reply)
Discussion started by: achu
1 Replies

9. Shell Programming and Scripting

how to grep and then to compare

Hello to all, i am new to scripting. I had to ques...1) If I had a variable $a which contains value i.e abc= jack I want to grep it first and then put into the variable lets say $b. Ques 2) If there is a file name.txt in which there are 15-20 names -----name.txt------ jack|male|london ... (5 Replies)
Discussion started by: ravi18s
5 Replies

10. UNIX for Dummies Questions & Answers

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... (2 Replies)
Discussion started by: lanna001
2 Replies
Login or Register to Ask a Question