Match and count the number of times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match and count the number of times
# 1  
Old 05-24-2010
Java Match and count the number of times

ile1

Beckham

Ronaldo

file2

Beckham Beckham_human

Ronaldo Ronaldo_spain

Ronaldo Ronaldo_brazil

Beckham Beckham_manch

Zidane Zidane_Fran

Rooney Rooney_Eng
Output shud be

Beckham 2

Ronaldo 2
Code:
 !usr|bin|perl -w

open(FILE1,"file1");
@array1=<FILE1>;
open(FILE2,"file2");
@array2=<FILE2>;
close(FILE1);
close(FILE2);

foreach $code(@array1)
{
        foreach $line(@array2)
        {
         
         chomp($code);
        
        @array2 =~ /$code/;
        $x += @array2;
                
}
}

print "$x\n";

# 2  
Old 05-24-2010
Code:
$
$
$ cat file1
Beckham
Ronaldo
$
$ cat file2
Beckham Beckham_human
Ronaldo Ronaldo_spain
Ronaldo Ronaldo_brazil
Beckham Beckham_manch
Zidane Zidane_Fran
Rooney Rooney_Eng
$
$
$ perl -lane 'chomp; if ($ARGV eq "file1"){$x{$F[0]}=1} elsif (defined $x{$F[0]}){$y{$F[0]}++}
              END {foreach $k(keys %y){print "$k => $y{$k}"}}' file1 file2
Beckham => 2
Ronaldo => 2
$
$

tyler_durden
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. Shell Programming and Scripting

Count same word which has come many times in single lines & pars

can i get a simple script for , Count same word which has come many times in single lines & pars Eg file would be == "Thanks heman thanks thanks Thanks heman thanks man" So resullt should be Thanks = 5 heman=2 man = 1 thanks in advance :) Please use code tags for code and... (1 Reply)
Discussion started by: heman96
1 Replies

3. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

4. Shell Programming and Scripting

Count how many times in every file, strings appeared in a directory.

Hello, I have some files and i want to count how many times a string is appeared in each file. Lets say : #cat fileA stringA sdh stringB stringA #cat fileB stringB stringA sdb stringB stringB I need the output to be something like: (2 Replies)
Discussion started by: @dagio
2 Replies

5. Shell Programming and Scripting

Count number of match words

Input: some random text SELECT TABLE1 some more random text some random text SELECT TABLE2 some more random text some random text SELECT TABLE3 some more random text some random text SELECT TABLE1 some more random text Output: 'SELECT TABLE1' 2 'SELECT TABLE2' 1 'SELECT TABLE3' 1 I... (5 Replies)
Discussion started by: chitech
5 Replies

6. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

7. Shell Programming and Scripting

The loop was executed $count times

#!/bin/sh count=0 for i in 2 4 6 do echo "i is $i" count='expr $count + 1' done echo "The loop was executed $count times" with these scripts my output is : i is 2 i is 4 i is 6 The loop was executed expr $count + 1 times What should I do to get the value instead of 'expr... (17 Replies)
Discussion started by: ymwong
17 Replies

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

9. Shell Programming and Scripting

scripting - write a script that will count the number of times a particular word

hello everyone, I'm trying to learn some scripts but i cant get my head around two of them. 1. how can i write a script that will count the number of times a particular word is used in file? 2. how can i make a script that will take me to a web page from unix? if anyone could help it... (3 Replies)
Discussion started by: BigTool4u2
3 Replies

10. Shell Programming and Scripting

count times for one string

I have a file. I want to count the time for one string appears in this file Example: 56 73 34 79 90 56 34 Expected results 2:56 1:73 2:34 (1 Reply)
Discussion started by: anhtt
1 Replies
Login or Register to Ask a Question