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

Recommended Answers

All 2 Replies

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;

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!

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.