Please do help: Perl Script to pull out rows from a CSV file

I have CSV file that contains data in the format as shown below:

ABC, 67, 56, 67, 78, 89, 76, 55
PDR, 85, 83, 83, 72, 82, 89, 83
MPG, 86, 53, 54, 65, 23, 54, 75
..
..
..
..

I want to create a script that will pull out the rows from the above sheet and paste it into another CSV file. The inputs to the script would be: suppose a name HGR which I will specify and tell that it corresponds to PDR. So the new CSV file will contain HGR and in front of it will have the numbers 85, 83, 83, 72, 82, 89, 83 pasted in front of it. Similarly I should be able to specify multiple such names at a time.

Can anyone help me on this? I am a hardware engineer and dont know much about scripting.

can u specify an example of the output you want

Input file:

ABC, 67, 56, 67, 78, 89, 76, 55
PDR, 85, 83, 83, 72, 82, 89, 83
MPG, 86, 53, 54, 65, 23, 54, 75
DPG, 86, 97, 95, 84, 89, 84, 75
PQT, 66, 73, 94, 45, 63, 74, 85
..
..
..
..[LEFT]

I will have a per script which takes the following inputs:

MHF = ABC
JHT = DPG

The per script runs and I get the output in the following format:

MHF, 67, 56, 67, 78, 89, 76, 55
JHT, 86, 97, 95, 84, 89, 84, 75

Can you help with a code?
[/LEFT]

Hi,

Try This.

#!/usr/bin/perl

$filename = shift;

while (@ARGV) {
$text1 = shift;
$text2 = shift;

$ret=`grep $text2 $filename`;
chomp $ret;
$ret =~ s/$text2/$text1/g;

print $ret,"\n";

}
perl perlscript.pl filename HGR PDR DDD MPG

Output

HGR, 85, 83, 83, 72, 82, 89, 83
DDDD, 86, 53, 54, 65, 23, 54, 75

It would be great to have that script take the inputs of filename HGR PDR DDD MPG from within the perl script and then output the data as a .csv file which when opened using Excel would give me HGR and DDD in one column but two different rows and the data 85, 83, 83, 72, 82, 89, 83 in the same row as HGR but in different columns and similarly for DDD.

Thanks a lot. :slight_smile: :):slight_smile:

Hi,

You will get the output as expected, Please find the attached csv file.

Hi Pravin,

I am sorry but where's the attached CSV? I did not understand that.

The result from the code that you gave me is output on the command line of the unix system. I wanted this output in a separate CSV file in multiple rows.

Please do let me know.

Try this ....

#!/usr/bin/perl


open (FW, "> outputfile.csv");
$filename = shift;

while (@ARGV) {
$text1 = shift;
$text2 = shift;

$ret=`grep $text2 $filename`;

chomp $ret;
$ret =~ s/$text2/$text1/g;

print FW $ret,"\n";

}

close (FW);

If you have a reference file like this:

ref.file:

MHF ABC
JHT DPG

you can do something like:

awk -F, 'NR==FNR{a[$2]=$1;next}
a[$1]{$1=a[$1]}1
' OFS="," ref.file input.file > newfile.csv

Hi Pravin,

Thanks a lot for that you.

It would be great to have the inputs i.e. 'the input CSV file', the 'HGR' 'PDR' 'DDD' 'MPG' from within the perl script instead of the command line.

I understand am asking a lot, but that was the intention since there would be atleast a 100 such inputs which I would be giving.

could you post your input csv file.

perl, just modify below hash1 definition should be ok to address your issue

temp.pm

package Temp;
sub TIEHASH{
	my $class = shift;
	my $file= $_[0];
	my %hash;
	open FH,"<$file";
	while(<FH>){
		chomp;
		my @tmp = split(",",$_,2);
		$hash{$tmp[0]}=$tmp[1];
	}
	close FH;
	my $self ={};
	$self->{DATA}=\%hash;
	$self->{KEY}=$_[1];
	return bless $self,$class;
}
sub FETCH{
	return $_[0]->{DATA}->{$_[0]->{KEY}->{$_[1]}};
}
1

a.pl

use Temp;
my %hash1=(KEY1=>'PDR',KEY2=>'ABC',KEY3=>'MPG');
tie %hash,"Temp","yourfile.txt",\%hash1;
print $hash{'KEY1'},",KEY1\n";

@Pravin - The input csv file is as mentioned below

ABC, 67, 56, 67, 78, 89, 76, 55
PDR, 85, 83, 83, 72, 82, 89, 83
MPG, 86, 53, 54, 65, 23, 54, 75
..
..
..
..

When I use the code that you gave me, I have to mention HGR PDR DDD MPG on the command line. Instead, it would great to give these in the perl script file or any other file and I just put perl perlscript.pl filename on the command line.

Can you help with the modified code?