The above is a single line in file2. there are many lines like this.
So i need to check compare the file1 with file2(1st column). If that info matches in file 2 then i need to paste file 1 in file2 1st column and othe other coulumn in file2 should remain same without any space modifications.
Most important is records present in file 1 should be there in file2 and other lines in file 2 should be deleted in file2.
Please give me the soulution to resolve this problem.
#!/usr/bin/perl
use warnings;
use strict;
my %file2;
open (FILE2, '<' , 'path/to/file2') or die "$!";
while(<FILE2>){
$file2{(split(/\s+/))[0]}=1;
}
close FILE2;
open (FILE1, '<' , 'path/to/file1') or die "$!";
open (OUT, '>' , 'path/to/outputfile') or die "$!";
while(<FILE1>){
chomp;
if (exists $file2{$_}) {
print OUT "$_\n";
}
}
close FILE1;
close OUT;
You want to read the smaller of the two files into a hash, so if file1 is considerbaly smaller than file2, file1 should be read into a hash to compare to file2 instead of how I did it. If both files are the same or nearly the same size it does not matter.
nivas
(1) sort the first field of file1
(2) same way sort the second file on the first field
(Assuming the first field's record length is same if digit is there use sort -n
if strings is there use sort -d and if both are there use sort -k , and you want to join on the first field.)
use the comd ->
join -1 1 -2 1 -t(the delimiter used ) -o 1.1 1.2 2.1 2.1 file1 file2 > file 3
Here 1.1 is the first filed of file1 and likewise .I hope it I have made it
clear .