Hi Perl guru's

My name is Omer and i am almost new on perl i need help for below mentioned issue.

Well,

I have two files

file A & file B

file A have two columns and it looks like

ColumnA ColumnB
A 01
B 02
C 03

and File B has one column and it looks like

ColumnA

01
02
03
04
05
06
...
...
...

Now i what to do is

Match the column of file B with the columnB of file A if both values matches then display the value of column A in file A.

guys i need your help infact a temaplate code because i am not a perl guru.

I hope that you guys will help me out to fix this issue.

Thanks in advance.

Omer

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $fa = 'a.txt';
my $fb = 'b.txt';

my %h; #Hash to store file 'a' data
# First, open and read file 'a' into a hash
open (my $fha, '<', $fa) or die "Unable to open $fa";

while (<$fha>){
    next unless m/\d+/; #Skip this line if it doesn't contain a number
    my @data = split;
    $h{$data[1]} = $data[0];
}
close $fha;
#print Dumper(\%h);

open (my $fhb, '<', $fb) or die "Unable to open $fb";
while (<$fhb>){
    chomp;
    next unless m/\d+/; #Skip this line if it doesn't contain a number
    next unless exists $h{$_}; #Skip this line if no hash key for this number
    print "$_ from file B => $h{$_} from file A\n";
}
close $fhb;

Anther One Way using Regex

use strict;
use warnings;

### Read & store the whole file in $filea
open (FILEA, "a.txt") || die "Cannot open the file a.txt : $!";
read FILEA, my $filea, -s FILEA;
close (FILEA);

### Read the file B
open (FILEB, "b.txt") || die "Cannot open the file b.txt : $!";

### line by line processing for File B
while (<FILEB>)
{
	chomp($_);
	print "\n$_ from file B => $1 from file A" if ($filea=~ m{\s*(.*?)\s+($_)});
}

Hi Guys,

Thanks for your reply before i go to the above mentioned issue i need help on one more issue.

I have a text fileA with two columns.

Column A Column B

ABC 09
ABC 56
DEF 98
DEF 87
DEF 89
DEf 55
GHI 67
GHI 56
JKL 98
MNO 90

Now i want to do is

read the first column from fileA and if the next line of columnA is similar with line-1 then add 1 in the counter and then display the result although i have to write to code but it is not working.
could you guys please help me out with this..?

Thanks in advance

Hi Guys,

Thanks for your reply before i go to the above mentioned issue i need help on one more issue.

I have a text fileA with two columns.

Column A Column B

ABC 09
ABC 56
DEF 98
DEF 87
DEF 89
DEf 55
GHI 67
GHI 56
JKL 98
MNO 90

Now i want to do is

read the first column from fileA and if the next line of columnA is similar with line-1 then add 1 in the counter and then display the result although i have to write to code but it is not working.
could you guys please help me out with this..?

Thanks in advance

In the future when you have a new question please start a new thread to ask it. That makes it easier for someone searching for the answer to a question similar to your new one. For now, you can try the following:

#!/usr/bin/perl
use strict;
use warnings;

my $fh; #Declare a filehandle
if (-e 'a.txt'){
    open ($fh, '<', 'a.txt') or die "Can't open 'a.txt': $!";
}
else{
    #If a.txt doesn't exit in working dir, read data from DATA section
    #by aliasing the filehandle (convenient for testing)
    $fh = *DATA;
}

my %h; #Declare a hash to count column A values
while(<$fh>){
    next unless m/\d+/;#Skip lines containing no digits
    my ($col_a, $col_b) = split;
    $h{$col_a}++;#Count occurence of this value
}

foreach(sort keys %h){
    my $s = ($h{$_} == 1) ? '' : "s";
    print "$_ occurs $h{$_} time$s\n";
}

__DATA__
Column A Column B

ABC 09
ABC 56
DEF 98
DEF 87
DEF 89
DEf 55
GHI 67
GHI 56
JKL 98
MNO 90

Hi d5e5

Thanks for your reply :)

I have written a below code to take the total of each matching ocurrance but this code only works with one single column when i try to run it on two columns file it does not work could you please have a look into it and please let me know how i can make it to work on two columns file.

here is the code

#!/usr/local/bin/perl

use strict;
use warnings;
use Config;
use Getopt::Std;

my %hashGroups;
my $strOS = $Config{osname}; #print "$strOS\n";
my $k;
my $v;
my $key;
my $NV_HOME = "/usr/netvault";
# Who to mail
my $CONFIGFILE;
my $whotomail = "email receiver ";

#get path of this script
use Cwd qw(abs_path);
my $strMyPath = abs_path($0);
my $nPos = 0;

#read flat file
&ReadFile($ARGV[0]);
system("rm /opt/backup/bin/Data_File.txt");

open (LOGON,">>",'Data_File.txt') or die "Could not open file Data_File.txt:$!";

#now print values hash
#    print LOGON "Group               Count";
print "\n";

foreach $key (sort (keys(%hashGroups)))
{
#   print LOGON "         Group  \t\t\tCount: $hashGroups{$key}\n\n";
#   print  LOGON "$key";print LOGON "              $hashGroups{$key}\n";
#my ($key,$hashGroups{$key});




format LOGON_TOP = 
Netvault Media Details    Page: @>>>>>>>
                                $%


Group Name                Total Media Count
-------------------------------------------
.

format LOGON = 
@<<<<<<<<<<<<<<    @#############
$key,              $hashGroups{$key}
.


write(LOGON);

}


#while ( ($k,$v) = each %hashGroups )
#{
#       print "Groups: $k => count: $v\n";
#}

close LOGON;
exit;   #**MAIN END**

sub ReadFile
{
        my $strFile = (@_)[0];
        my $i =0;
       

        #print "$strFile\n";
      	#if (-e '$strFile')
	#{
        open (CONFIGFILE,'<',"$strFile") || die ("Could not open file!");
        #}
	#else
	#{
	#	$CONFIGFILE = *DATA;

	#}
	while (<CONFIGFILE>)
        {
		
                
               	chomp;
		next if /^\s*$/;
                
		
		
                if ($i >= 3)
                {
		my $strLine = $_;
                my $strLine2 = trim($strLine);

		if ($strLine2 =~ /Component*/)
                {
                        next;
                }
		if ($strLine2 =~ s/Unknown/Blank/)
                {
                      #  next;
                }

                if ( index($strLine2, '#', 0) ge 0 )
                {
                        next;
                }
                
                if ( exists($hashGroups{$strLine2}) )
                {
                        #print "Found $strKey\n";
                        $hashGroups{$strLine2} = $hashGroups{$strLine2} + 1;            #increment value in HASH
                        #print ($hashGroups{$strKey});
                        #print "\n";
                }
                else
                {
                        $hashGroups{$strLine2} = 1;                                                                     #store value in HASH
                }
                }       $i++;
        }

        close (CONFIGFILE);
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}
system ("cat /opt/backup/bin/Data_File.txt | \"$NV_HOME/util/nvsendmail\" -f - -s \"Report\" -d \"$whotomail\"");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.