senthilamp4 0 Unverified User

Hi Experts,
Here i have trying and written the perl script for extract the information from csv file and create the output to another csv file. Contents are occured in row by row format in attached input file and also i have attached corresponding out file also.

my $dir = "C:\\Documents and Settings\\senthilv\\Desktop\\sample\\input\\";

	my $current_logs;
 	opendir(DIR, $dir) or die $!;
	while (my $file = readdir(DIR))
	{

		next unless (-f "$dir\\$file"); # Here find the files only
		next unless ($file =~ m/\.csv$/gi);
		push (@{$current_logs}, $file);
	}
	closedir(DIR);


my @header= ();

foreach $each_file(@{$current_logs})
{
	my (@header_val, @modified_header,@tables) = ();
	my %seen;
	my %hash_array;
  ($date=$1, $cur_time=$2) if($each_file =~ m/_(\d+)_(\d+)\./g);

    open (my $IN, '<', $input_dir.$each_file) or die "cannot open $each_file for writing: $!";
    local $/ = "Table=";  # Based on the 'RTRV' and split the metric groups
    my @all_metrics= ();
    while( my $record = <$IN>)
    {
    chomp;
	$record =~ m/Label(\s+)(.*)(\s+)Value/gi;
	my $header_val = $2;
	push (@header,split(',',$header_val));
	push (@tables,$record);
	}

my @modified_header = map { "$_," } @header; 
open ( my $outFileHandle, '+>', "sample.csv") or die "cannot open $newFile for writing: $!";
print $outFileHandle " @modified_header,";
print $outFileHandle "\n";

my @body=();
my @convert_body= ();
foreach my $each_table_grp(@tables)
{
	$each_table_grp =~ m{Value(.+?)Table}gis;
	push(@body, $1);
}
		my @unique_body = ();
		foreach my $elem(@body)
		{
			next if $seen{ $elem }++;
			$elem =~ s{,Value}{}gis;
			push (@unique_body, $elem);
		}

		foreach my $body_val(grep /(.+)/, @unique_body)
		{
			print $outFileHandle "$body_val\t"
		}
}

Thanks in Advanced,
Senthil. V

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.