Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

digital1

macrumors 6502
Original poster
Jan 2, 2002
294
0
Wisconsin
I have the following code:

open (epiphanyInput,"input2.csv");
my(@epi_list)=<epiphanyInput>; #read file into list

my($epi_list_line); #declare a line reader so that when you loop you can grab results

#Read in the second file for writing to
open (epiphanyOutput,"+>>Epiphany_results.txt");
my(@epi_result)=<epiphanyOutput>;
my($epi_result_line);

#This code goes through file input from one list and if it's not found in
#Epiphany results it is put into the Epiphany results file.
foreach $epi_list_line(@epi_list){
$search_result=qx(grep "$epi_list_line" Epiphany_results.txt);
#print $search_result;
#print $epi_list_line;


#If these are equal, do nothing at all,else write out to the file the name of the program
if(lc($epi_list_line) eq lc($search_result)){
print "0";
}


if(lc($epi_list_line) ne lc($search_result)){
#print "2";
print epiphanyOutput "$epi_list_line";
}



What I am trying to do is look through one text file that has the results of a system-run PS command based off of another text file that keeps track of all commands/programs ever run by PS for some experimentation I am doing for college. In my $search_results variable I am attempting to store the result of that search. I use qx() to start up grep. Instead though of grep returning the instance of the search_result,It's like grep returns the entire contents of the file and I dont know why. I have tested the searches in command line and they come up with results I am looking for, but when I run them in the way I have it in Perl, it returns the entire contents of the original text file that I searched. You guys have any ideas on how I can search a text file in Perl in a straightforward manner in Perl? I have looked into Regular Expressions, but I am not sure how I would approach this yet. Any help would be appreciated. Thanks in advance!

P.S. Ignore some of the comments I have in there right now. The comments are actually my goal for what I want a line to do. If you have any ideas let me know as well!
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
Why are you spawning a grep in order to match a line?

Perl's Regexp are really great..

here's some code to replace your qw...

PHP:
open(SCAN, "Epiphany_results.txt" || die("Could not open file!");
@scanned=<SCAN>;
close(SCAN);

foreach $epi_list_line(@epi_list){
  foreach (@scanned){
    if( /$epi_list_line/ ){
     print $s_;

    }
  }
}

There must be a quicker way to do this, but my perl days are waaaay behind my poor memory ;)
 

mwpeters8182

macrumors 6502
Apr 16, 2003
411
0
Boston, MA
I forgot how ugly other people's perl code is to look at. And that's not a knock on you, that's a knock on perl). There's just too many ways to do something.
 

digital1

macrumors 6502
Original poster
Jan 2, 2002
294
0
Wisconsin
Thanks for the responses! :)
When I tried running your code, I get blank output. :-\ It's like it is not going through the inner forloop. I tried displaying output, but nothing from that inner loop gets displayed. :-\
 

demallien

macrumors regular
Oct 13, 2005
137
0
digital1 said:
I have the following code:

...Lots of ugly Perl code deleted...

Any help would be appreciated. Thanks in advance!

Scrap the lot and do it in Ruby :)
 

snej

macrumors member
Apr 15, 2003
33
8
Removing duplicate lines from a file

If the order doesn't matter, sort -u should do the trick.
If you read all lines to memory first, you can remove the dupes from the list before writing to the destination file. You'll find samples how to do that in the perl documentation: perldoc -q duplicate.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.