954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help needed on Search and Print if String exists

Hello,

I've searched far and wide for an answer to a question that I have, but did not find anything that could help me achieve what I would like to do.

I have a delimited text file (CSV) with fields that resemble these below that repeats/resemble 18 times (I only included 3 rows for an example).

It's a regularly updated database where Row-1, 2 or 3 etc, stand to change from "open" to "closed".

closed.gif;Row-1;open.gif;closed.gifclosed.gif;Row-2;closed.gif;closed.gifclosed.gif;Row-3;open.gif;closed.gif

With the (working) code I have below, I would like the script to "print" the output, but only if the string "open.gif" exists on any given line, and NOT printing if it is not.

It would be appreciated, thank you.

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);
use strict;

my $csvfile = "/my_file.txt";

print "Content-Type: text/html\n\n";

open (CSV, "< $csvfile");
while( <CSV> )
{
my @cols = split(/;/, $_);


if (2..18) {print "Row-1: $cols[0]Row-2: $cols[1]Row-3: $cols[2]"

}

}
close CSV
fredfletcher
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

try this (untested) - hope it helps!

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);
use strict;

my $csvfile = "/my_file.txt";

print "Content-Type: text/html\n\n";

open (CSV, "<$csvfile") or die "Can't open $csvfile\n";
while( <CSV> )
{
chomp;
my $line = $_;
if ($line =~ 'open.gif'){
my @cols = split(/;/, $line);
print "Row-1: $cols[0]Row-2: $cols[1]Row-3: $cols[2]\n";
}
}

close CSV
bio-grad
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 1
 

Thank you (bio-grad), it works beautifully! cheers

fredfletcher
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You