Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matching an expression with grep 1

Status
Not open for further replies.

mikeprestim

Programmer
Mar 1, 2001
21
GB
Hi

I have an external file from which i need to see if the file contains the chrs $$exp.

My code line is

my $i = system "grep '$$exp' $ARGV[0]";

$i is 0 if true

fails because $exp is not defined, but i do not want to i am checking for an explicit existance of $$exp in an external file. I have tried \$\$.exp $.$.exp

any ideas. Also correct perl syntax of grep would be nice so i do not need to do system

Thanks
Mike
 
Hello Mike

Try this:
Code:
while(<>){
  print if /\$\$exp/;
}
That 3 line perl script will do what you ask. To do the job as part of a larger script you'll need to be a bit more explicit, something like this:
Code:
$InFile=0;
$f = $ARGV[0];   # get the filename from the command line
open(F,$f);      # open the file
while(<F>){      # read through each line of the file
  if(/\$\$exp/){ # check each line of the file
    $InFile=1;   # found it.
    last;        # leave the loop, no need to look anymore
  }
}
close F;         # close the file
if($Infile){
  print '$$exp found in file ',"$f\n";
} else {
  print '$$exp NOT found in file ',"$f\n";
}
I haven't run the code :)

Mike

Hardware is that part of a computer which, when you remove electrical power, doesn't go away.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Thanks Mike. Sorted works a treat. Only one small typo Infile instead of InFile in the test condition at the end.

Its useful code as it helps out in the next part of the project. Its just error trapping stuff in a programmable CAM environment

Thanks again
Mike
 
My first star in ages :) ta.

Probably better if you wrap that code in a little subroutine, returning 1 or 0 like the value of $In[fF]ile.


Mike

Hardware is that part of a computer which, when you remove electrical power, doesn't go away.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top