Counting Occurances in Two Files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting Occurances in Two Files
# 1  
Old 06-25-2001
Question Counting Occurances in Two Files

I have two files I want to compare, one is a list of variables and the other is a text file COBOL program.
Basically what I want to do is display only those variables that appear in the COBOL program only once. However I would also accept a count of each variable as it appears in the COBOL program.

fgrep -f VAR TEST.CBL
will display the lines of the code that match the variable file, however what I need is to locate those variables that have only one match.

Any ideas?
# 2  
Old 06-26-2001
I dont think you can do it directly with grep. How about:

Code:
#!/bin/ksh
for f in `cat VAR`
do
  COUNT=`grep $f TEST.CBL | wc -l`
  if [[ $COUNT -eq 1 ]]
  then
     echo $f
  fi
done

# 3  
Old 06-26-2001
Works great!
Thank you PxT!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 Replies

2. Shell Programming and Scripting

Count the number of occurances for multiple files

I have some text files as shown below. I would like to add the values of each string. Your help would be appreciated!! file1.txt SUS 2 PRS 2 ALI 1 PRS 1 GLK 2 file2.txt PRS 3 GLK 6 SUS 18 Desired output SUS 20 PRS 6 (2 Replies)
Discussion started by: arch
2 Replies

3. UNIX for Dummies Questions & Answers

Counting files without ls or wc

i need to write a shell script to "count the number of files in the current directory but without using either ls or wc command"..... please help! (1 Reply)
Discussion started by: lexicon
1 Replies

4. Shell Programming and Scripting

Counting Files

In a script, how would I go about finding the number of files for the first parameter after my script name? For instance, my script name is myscript.sh and the folder I am checking is not the current working directory, lets say it's folder1. so I type myscript.sh folder1 This script below... (2 Replies)
Discussion started by: Confirmed104
2 Replies

5. Shell Programming and Scripting

multiple files: counting

In a directory, I have 5000 multiple files that contains around 4000 rows with 10 columns in each file containing a unique string 'AT' located at 4th column. OM 3328 O BT 268 5.800 7.500 4.700 0.000 1.400 OM 3329 O BT 723 8.500 8.900... (7 Replies)
Discussion started by: asanjuan
7 Replies

6. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

7. Solaris

Counting up files

Hi, I have a load of if statements that look for files in a directory, I want to be able to count them up and the total files confirmed in an email? I ahve tried expr but i this does not work and it only reads in the first if and ignores the rest. Please see script, #!/bin/ksh ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

8. Shell Programming and Scripting

Help with counting files please

Hi all. If I have a unix directory with multiple files, lets say, I have some with .dat extensions, some with .txt extensions, etc etc. How in a script would I provide a count of all the different file types (so, the different extensions, I guess) in the directory?? So if I had: test.dat... (6 Replies)
Discussion started by: gerard1
6 Replies

9. UNIX for Dummies Questions & Answers

counting files

Which one line command can count and print to the screen the number of files containing "a" or "A" in their name (5 Replies)
Discussion started by: Edy
5 Replies
Login or Register to Ask a Question