0

Hi,

I am trying to get to use the grep function in perl,
I have been searching web pages but can't get a simple example.

I am reading in a .csv file with 7 columns and loads of rows, this is done with:

$dat_file = 'sample.csv';

In the 3rd or 5th column I could find the information I want for example it might have a manufactured such as say nokia.

I want to create a new .csv file that just has all rows that contain nokia.

I did this very easily in ubuntu with just grep nokia sample.csv > nokia.csv
Does anyone know how to do this simply in perl?

Thanks,

N

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by niall_heavey
1

Use a regular expression instead. It's like grep and as powerful.

here's an example that searches the entire line for "nokia":

use strict;
use warnings;
open (FILE,"<","sample.csv");
open (OUT, ">","nokia.csv");
while(<FILE>){
 print OUT if(/nokia/i);
}
close FILE;
close OUT;
0

Use a regular expression instead. It's like grep and as powerful.

here's an example that searches the entire line for "nokia":

use strict;
use warnings;
open (FILE,"<","sample.csv");
open (OUT, ">","nokia.csv");
while(<FILE>){
 print OUT if(/nokia/i);
}
close FILE;
close OUT;

Brilliant, that works perfect. Exactly what I was looking for!

Thanks a million!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.