Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
merge two CSV file's data in one CSV File

Posted by ksamir2004 (ksamir2004), 21 February 2008
Hi all,

How to merge two CSV file data in one One CSV file  use of perl script..
its ugren..

Thanks
Sam

Posted by admin (Graham Ellis), 21 February 2008
Hello - we don't do "urgent" at the free service here ... if you would like to post up some sample code showing how far you have got, we'll do our best to provide some guidance within our stated targets of 24 hours.

Posted by KevinAD (KevinAD), 21 February 2008
merge means nothing out of context, but I have a feeling this person is going to be a no show so I don't want to put any effort into helping unless he comes back and defines the problem and posts his code.

Posted by ksamir2004 (ksamir2004), 22 February 2008
Hi,

Suppose i have two CSV file. in 1st file there is some content and in 2nd file fields are same but few contents different.
i need to merge both content and finally create 1 csv file using perl..

plz help us..
Sam

Posted by admin (Graham Ellis), 22 February 2008
Sam, as Kevin says, we need to undertstand just how the merge is to work - whether it's keyed on particular fields or all unique records are to be placed in the output, if keyed which duplicate record is to be placed in the output, what is to be done about the order of records if they're different in the two .csv files, and so on.  

If you can provide a short sample showing the tow data files input and what output ypu need, and also where you've got with coding a solution so far, we can help you forward. Without those inputs, I can confidently predict that Kevin, I or others could write you a comple answer and your email in reply will start off Thank you but that's not what I wanted to do ....  We're happy to help provide a solution, but as it stands at the moment all we would be doing is wasting our time - we don't really know the detail of the question!

Posted by ksamir2004 (ksamir2004), 22 February 2008
Hi Graham Ellis/Kavin,

i m explaining our requirement..

suppose i have two CSV file.  

Loginf.cvs:

IP                   Domain          Port           Desc
121.1.1.0    xyz.ss.com        1023         Config file
212.1.1.1   abc.sww.com     1211          xxx file
211.1.12.1   bcd.com            80              conf file

Logback.cvs:

IP                   Domain           Port           Desc
12.1.1.0        ss.com            1020         Config file
21.1.1.1        sww.com        1211          rrr file
21.1.12.1   bcd.com              80              confr file

here output should store on different file.

output.csv

IP                   Domain          Port           Desc
121.1.1.0    xyz.ss.com        1023         Config file
212.1.1.1   abc.sww.com     1211          xxx file
211.1.12.1   bcd.com            80              conf file
12.1.1.0        ss.com            1020         Config file
21.1.1.1        sww.com        1211          rrr file
21.1.12.1   bcd.com              80              confr file



Posted by ksamir2004 (ksamir2004), 22 February 2008
Hi Graham Ellis/Kavin,

i need to merge two csv file in one csv file & suppose IP address are same and remaining data is diff then it should print Number of times IP.

i am atteching Perl script.. its not counting IP. can you help me to modify this script.
---------------------------------------------------------
use Cwd;
$dir = cwd;
print "\nEnter 1st File Name :--> \t";   ## Give 1st File Name.
$file1=<STDIN>;
print "\nEnter 2st File Name :--> \t";  ## Give 2st File Name.
$file2=<STDIN>;
@files = ($file1,$file2);
print "\nEnter File Name For Creating Report:--> \t";
$s=<STDIN>;
for ($i = 0; $i < 2; $i++) {

open MARGIN, "<$files[$i]" or die "File not found: $!";
     while (<MARGIN>) {
     chop;

   (@content) = split /, /;

     $prod_margin{$content[0]} = \@content;
     }
     close MARGIN;
     }

open MARGIN, ">$s" or die "Please Close $s File And Run Again: $!";
for $line (keys %prod_margin) {
print MARGIN $line,"\n";
}
close MARGIN;
print "\nFile has Created in $dir/$s\n";
-----------------------------------------------------------

suppose i have two CSV file.  

Loginf.cvs:

IP                   Domain          Port           Desc
121.1.1.0    xyz.ss.com        1023         Config file
212.1.1.1   abc.sww.com     1211          xxx file
211.1.12.1   bcd.com            80              conf file

Logback.cvs:

IP                   Domain           Port           Desc
12.1.1.0        ss.com            1020         Config file
21.1.1.1        sww.com        1211          rrr file
21.1.12.1   bcd.com              80              confr file

here output should store on different file.

output.csv

IP                   Domain          Port           Desc
121.1.1.0    xyz.ss.com        1023         Config file
212.1.1.1   abc.sww.com     1211          xxx file
211.1.12.1   bcd.com            80              conf file
12.1.1.0        ss.com            1020         Config file
21.1.1.1        sww.com        1211          rrr file
121.1.1.0    xyz.oo.com        1020        Con file
21.1.12.1   bcd.com              80              confr file
21.1.12.1   bss.com              180             wconfr file


21.1.12.1 -> 2 times
121.1.1.0-> 2 times


Posted by admin (Graham Ellis), 22 February 2008
Thanks - I'm off to work in a few minutes, but I'll have a look tonight or over the weekend.   That's a much better description to work with and hopefully come up with an appropriate answer

Graham

Posted by KevinAD (KevinAD), 22 February 2008
see my post below

Posted by KevinAD (KevinAD), 22 February 2008
Your second example makes little sense unless there are errors in it. In the output you have:

121.1.1.0    xyz.oo.com   1020   Con file
21.1.12.1   bss.com    180   wconfr file  

but the above lines are not present in the input files.

It helps to post samples of your real data, including the commas if it is a CSV file.

Posted by admin (Graham Ellis), 23 February 2008
I'm afraid I'm baffled by the first example - there seems to be a domain name that's turned up that doens't exist in either of the two input files, and the output file is longer than the sum of the two input files .... so I think that the example is incomplete.

The second example seems to just merge the two files ...

open (FH,"firstfile");
@stuff = <FH>;
open (FHO,">output");
print FHO @stuff;
open (FH,"secondfile");
@stuff = <FH>;
print FHO @stuff;

Would produce the merged output.

Sam - I am suspecting we would be able to help you much better in person rather than via the forum;   I haven't checked where in the world you are, but I would be willing to spend an hour or two helping if you can pop by - http://www.wellhousemanor.co.uk



Posted by KevinAD (KevinAD), 23 February 2008
I think I understand, append the two files together in a third file and count the number of times each IP address is found in both files.

Untested code:


use strict;
use warnings;
use Cwd;
my $dir = cwd;
my %count;
print "\nEnter 1st File Name :--> \t";   ## Give 1st File Name.
chomp(my $file1 = <STDIN>);
print "\nEnter 2st File Name :--> \t";  ## Give 2nd File Name.
chomp(my $file2 = <STDIN>);  
print "\nEnter File Name For Creating Report:--> \t";
chomp(my $s = <STDIN>);
unless ($file1 && $file2 && $s) {
  die "Usage: perl scriptname.pl <file1> <file2> <outputfile>\n";
}
open MARGIN, ">", $s or die "Please Close $s File And Run Again: $!";
for ($file1,$file2) {
  open IN, "<", $_ or die "File $_ not found: $!";
  while (<IN>) {
     print MARGIN;
     my $ip = (split(/,/))[0];
     $count{$ip}++;
  }
  close IN or die "Can't close input file: $!";
}
print MARGIN "\n\n";
foreach my $ip (sort {$count{$a} <=> $count{$b}} keys %count) {
  print MARGIN "$ip --> $count{$ip}\n";
}
close MARGIN;
print "\nFile has Created in $dir/$s\n";


Posted by ksamir2004 (ksamir2004), 25 February 2008
Hi Graham Ellis & KevinAD,

Thank you very much Graham Ellis & KevinAD. I am from Singapore. Please keep in touch.. my email id is: ksamir2004@gmail.com

Thanks a lot both of you....

Thanks
Sam



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho