Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join CSV file

Status
Not open for further replies.

subok

MIS
Feb 21, 2005
37
BE
Hi,

I'm struggling for merge 2 large CSV file, the 2 file have a column with same information and will use this a key :

file1***
star,moon, car, house

file2,
cat,dog,horse,star

target result file:
star,moon,car,house,cat,dog,horse

would appreciate any help for this. I'm running Perl using windows

thanks.
 
Assuming your system has enough memory to load the entire file2 into a hash:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%file2[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] FILE2,[red]"[/red][purple]subok.file2[/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<FILE2>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[black][b]my[/b][/black] [blue]@a[/blue]=[url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple],[/purple][red]/[/red][red];[/red]
	[blue]$file2[/blue][red]{[/red][blue]$a[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red][red]}[/red]=[red]"[/red][purple][blue]$a[/blue][0],[blue]$a[/blue][1],[blue]$a[/blue][2][/purple][red]"[/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] FILE2[red];[/red]

[black][b]open[/b][/black] FILE1,[red]"[/red][purple]subok.file1[/purple][red]"[/red] or [black][b]die[/b][/black][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<FILE1>[red])[/red] [red]{[/red]
	[black][b]chomp[/b][/black][red];[/red]
	[black][b]my[/b][/black] [blue]@a[/blue]=[black][b]split[/b][/black] [red]/[/red][purple],[/purple][red]/[/red][red];[/red]
	[black][b]die[/b][/black] [red]"[/red][purple]key [blue]$a[/blue][0] missing from file 2[/purple][red]"[/red]	[olive][b]if[/b][/olive] ![url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]$file2[/blue][red]{[/red][blue]$a[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue],[blue]$file2[/blue]{[blue]$a[/blue][0]}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[black][b]close[/b][/black] FILE1[red];[/red]

You have some spaces in your example input data which I ignored, but if they occur in your key column you may need to handle or remove them explicitly.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top