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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please do help: Perl Script to pull out rows from a CSV file
# 8  
Old 04-20-2010
Try this ....

Code:
#!/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);

# 9  
Old 04-20-2010
If you have a reference file like this:

ref.file:
Code:
MHF ABC
JHT DPG

you can do something like:
Code:
awk -F, 'NR==FNR{a[$2]=$1;next}
a[$1]{$1=a[$1]}1
' OFS="," ref.file input.file > newfile.csv

# 10  
Old 04-20-2010
It works. :)

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.
# 11  
Old 04-20-2010
could you post your input csv file.
# 12  
Old 04-20-2010
perl, just modify below hash1 definition should be ok to address your issue

temp.pm
Code:
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
Code:
use Temp;
my %hash1=(KEY1=>'PDR',KEY2=>'ABC',KEY3=>'MPG');
tie %hash,"Temp","yourfile.txt",\%hash1;
print $hash{'KEY1'},",KEY1\n";

# 13  
Old 04-20-2010
@Summer_Cherry and Franklin52 - It did not work

@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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get duplicate rows from a csv file

How can i get the duplicates rows from a file using unix, for example i have data like a,1 b,2 c,3 d,4 a,1 c,3 e,5 i want output to be like a,1 c,3 (4 Replies)
Discussion started by: ggupta
4 Replies

2. Shell Programming and Scripting

Perl script to modify csv file

Hi Friends, I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file table findhost= { {"xyz","abc"}, {"rxz","mmz"}, {"vrr","nnz"}, } default={"NONE"} My current perl script #!/usr/bin/env perl... (12 Replies)
Discussion started by: dbashyam
12 Replies

3. Shell Programming and Scripting

Perl script to Convert XLSX or XLS files to CSV file

Hi All, I've got in a situation where I need to convert .xlsx or .xls formatted files into simple text file or .csv file. I've found many options but doing this using PERL script is the best way I believe.I'm in AIX box. Perl code should have 2 params while running. i.e perl... (1 Reply)
Discussion started by: manab86
1 Replies

4. Shell Programming and Scripting

Bash script help - removing certain rows from .csv file

Hello Everyone, I am trying to find a way to take a .csv file with 7 columns and a ton of rows (over 600,000) and remove the entire row if the cell in forth column is blank. Just to give you a little background on why I am doing this (just in case there is an easier way), I am pulling... (3 Replies)
Discussion started by: MrTuxor
3 Replies

5. Shell Programming and Scripting

pull out rows with specific criteria

Hi, I have a file with multiple columns and for column 5 I want to extract that row into another file if it is equal to or greater than a certain value. For example: FAR 4 5 7 LOP GAT 3 3 3 POL I want values that are greater than 5 for column 4. So the final file will look like... (1 Reply)
Discussion started by: phil_heath
1 Replies

6. Shell Programming and Scripting

Shell script to count unique rows in a CSV

HI All, I have a CSV file of 30 columns separated by ,. I want to get a count of all unique rows written to a flat file. The CSV file is around 5000 rows The first column is a time stamp and I need to exclude while counting unique Thanks, Ravi (4 Replies)
Discussion started by: Nani369
4 Replies

7. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

8. Shell Programming and Scripting

Calculate average from CSV file using PERL script

Hi All I have this csv file and I need to calculate the average of FPS. FPS:27.7420, Interval:1314184238772 FPS:25.9798, Interval:1314184242646 FPS:27.4772, Interval:1314184246311 FPS:26.1623, Interval:1314184250159 FPS:26.4515, Interval:1314184253972 FPS:31.5896, Interval:1314184257163... (24 Replies)
Discussion started by: sayachop
24 Replies

9. Shell Programming and Scripting

Deleting rows from csv file

Hello, I am supposed to process about 100 csv files. But these files have some extra lines at the bottom of the file. these extra lines start with a header for each column and then some values below. These lines are actually a summary of the actual data and not supposed to be processed. These... (8 Replies)
Discussion started by: cobroraj
8 Replies

10. Shell Programming and Scripting

Simple Script needed for Processing CSV file perl

I am new to perl, and need a script to pull a CSV file, strip out 2 leading columns, and 2 ending columns, and resave the file in a new location. The file is basic and has less than 10,000 records. Secondly, can I schedule perl scripts to run daily? Can someone provide the basic script to... (1 Reply)
Discussion started by: cobbjob
1 Replies
Login or Register to Ask a Question