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
# 1  
Old 04-20-2010
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.
# 2  
Old 04-20-2010
can u specify an example of the output you want
# 3  
Old 04-20-2010
Sure

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
..
..
..
..


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?
# 4  
Old 04-20-2010
Hi,

Try This.

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

}

Code:
perl perlscript.pl filename HGR PDR DDD MPG

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

# 5  
Old 04-20-2010
Thanks a lot Pravin

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. Smilie SmilieSmilie
# 6  
Old 04-20-2010
Hi,

You will get the output as expected, Please find the attached csv file.
# 7  
Old 04-20-2010
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.
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