Perl file comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl file comparison
# 1  
Old 05-07-2010
Perl file comparison

Hi,
I'm a newbie in perl scripting. I want to compare first 18 characters of two files using perl and write output if they matches.

FileA;
Code:
835100000000000020000000001000000.000000000.000
835100000000000120000000001000000.000000000.000
835100000000000230000000001000000.000000000.000
835100000000000300000000001000000.000000000.000
835100000000000320000000001000000.000000000.000
835100000000000380000000001000000.000000000.000
835100000000000550000000001000000.000000000.000
835100000000000570000000001000012.000000001.000

FileB:
Code:
83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000019020100504000012.318000006.568000013.284000012.532
83510000000000020020100504000017.733000011.877000016.389000015.547
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000050800000000000000.000000000.000000000.000000000.000
83510000000000051800000000000000.000000000.000000000.000000000.000
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978

If the first 18 characters matches, output file B.

OutputFileB:
Code:
83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978

Any help would be much appreciated.
# 2  
Old 05-07-2010
Code:
#!/usr/bin/perl

my $ainput = 'FileA' ;
my $binput = 'FileB' ;
my $boutput = 'OutputFileB' ;
my @temparr = () ;
my @mainarr = () ;

open(A,$ainput) or die "Error: $!\n" ;
open(B,$binput) or die "Error: $!\n" ;

my @aarr = <A> ;
my @barr = <B> ;

close(A) ;
close(B) ;

  foreach my $record (@aarr) {
  chomp $record ;
  my $sub = substr($record,0,18) ;
  @temparr = grep(/$sub/,@barr) ;
  push(@mainarr,@temparr) ;  }

open(BO,">$boutput") or die "Error: $!\n" ;
print BO @mainarr ;
close(BO) ;

# 3  
Old 05-07-2010
Quote:
Originally Posted by gpaulose
...
I'm a newbie in perl scripting. I want to compare first 18 characters of two files using perl and write output if they matches.
...
Here's another way:

Code:
$
$
$ perl -lne '$ARGV eq "filea" ? $x{substr($_,0,18)}++ : defined $x{substr($_,0,18)} ? print : ""' filea fileb
83510000000000002020100504000002.659000002.109000002.405000002.322
83510000000000012020100504000003.838000001.563000003.157000003.183
83510000000000023020100504000016.682000007.235000016.806000016.305
83510000000000030020100504000007.200000003.739000005.465000005.673
83510000000000032020100504000007.808000004.831000007.109000007.305
83510000000000038020100504000003.857000002.357000004.302000004.302
83510000000000055020100504000002.033000002.049000001.242000001.212
83510000000000057020100504000011.232000004.801000008.807000008.978
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 4  
Old 05-08-2010
another way,
Code:
# change the file names if requid.
open FILEA, '<filea';
open FILEB, '<fileb';

# get and store 
$hash{ substr($_, 0, 17) }++ while ( <FILEA> );

while ( <FILEB> ) {
    $part = substr ( $_, 0, 17 );

    print $_ if ( $hash{$part} );
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Comparison

HI, I have two files and contains many Fields with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields. this i wanted to use in shell scriting. ex: first.txt 111 |abc| 230| hbc231 |bbb |210 |bbd405 |ghc |555 |cgv second.txt 111 |abc |230 |hbc231... (1 Reply)
Discussion started by: prawinmca
1 Replies

2. Shell Programming and Scripting

Array elements comparison using perl

Experts, I am looking to compare elements of 2 array using perl. Below is not the actual code but logic wise something like this. my $version = "MYSQlcl-5.2.4-264.x86_64"; <-- split this word into array as (5 2 4 264) ( which is to extract only the version number from the package name) my... (1 Reply)
Discussion started by: solaix14
1 Replies

3. Shell Programming and Scripting

File Comparison: Print Lines not present in another file

Hi, I have fileA.txt like this. B01B02 D0011718 B01B03 D0012540 B01B04 D0006145 B01B05 D0004815 B01B06 D0012069 B01B07 D0004064 B01B08 D0011988 B01B09 D0012071 B01B10 D0005596 B01B11 D0011351 B01B12 D0004814 B01C01 D0011804 I want to compare this against another file (fileB.txt)... (3 Replies)
Discussion started by: genehunter
3 Replies

4. Shell Programming and Scripting

file comparison

Dear All, I would really appreciate if you can help me to resolve this file comparison I have two files: file1: chr start end ID gene_name chr1 2020 3030 1 test1 chr1 900 5000 2 test1 chr2 5000 8000 3 test2 chr3 6000 12000 4 test3 chr3 6000 15000 5 test3 file2:... (2 Replies)
Discussion started by: paolo.kunder
2 Replies

5. Shell Programming and Scripting

two file comparison

now i have a different file zoo.txt with content 123|zoo 234|natan 456|don and file rick.txt with contents 123|dog|pie|pep 123|tail|see|newt 456|som|sin|sim 234|pay|rat|cat i want to look for lines in file zoo.txt column1 that has same corresponding lines in column 1 of... (6 Replies)
Discussion started by: dealerso
6 Replies

6. Shell Programming and Scripting

Comparison of command line argument in perl

Hello All, I have passed one command line argument in my script. I want to compare that value in the script itself. Is there any way to compare the command line variable....... (3 Replies)
Discussion started by: suvenduperl
3 Replies

7. Shell Programming and Scripting

Comparison of two files which contains strings using Shell scripting or PERL

Hi, I need sample code to compare the two files line by line which contains text strings and to print the difference in the third file. Thanks in advance (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

8. Shell Programming and Scripting

File Comparison- Need help

I have two text files which have records of thousand rows. Each row is having around 40 columns. Each column is tab delimited. Each row is delimited by newline character. My requirement is to find for each row i need to find whether any column is different between the two files. For each row i... (8 Replies)
Discussion started by: uihnybgte
8 Replies

9. Shell Programming and Scripting

file comparison

hi I have 2 files to comapre ,in file a sible column it is numbers,in file b2 numbers and other values with coma separated. i want compare numbers in file a with file b,and the out put put should be in C with numbers in both file a and b along with other columns of file b. i used folowing... (7 Replies)
Discussion started by: satish.res
7 Replies

10. UNIX for Advanced & Expert Users

perl: string comparison as numbers

$name = 'Mark'; $goodguy = 'Tody'; if ($name == $goodguy) { print "Hello, Sir.\n"; } else { print "Begone, evil peon!\n"; } The output is Hello, Sir...... Even if the Mark is made to MarkSpencer.....then also it shows as Hello, Sir.... Can any one explain the... (7 Replies)
Discussion started by: bishweshwar
7 Replies
Login or Register to Ask a Question