Re: AW: Testing File Contents
On 2011-03-02 19:22, Christian Marquardt wrote: open CFG, '', $_file || die(could not open file: $_file!); That only dies if $_file is false. Funny that you did use parentheses with die. Some '$' characters were missing too: open my $CFG, '', $_file or die Error opening '$_file', $!; -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On Mar 2, 9:55 am, lm7...@gmail.com (Matt) wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? Yet another way: perl -0777 -nlE ' say /mystring/ ? yes : no ' file See: perldoc perlrun for explanation of -0777 -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Testing File Contents
I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
# Untested use strict; use warnings; open my $fh, '', my_file; while($fh){ if ($_ !~ /my_string/) { # Do something } } The other way would be on shell - # Untested grep my_string my_file if [ $? -eq 1 ] then echo Do something fi ~Parag On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On Wed, Mar 2, 2011 at 10:11 AM, Parag Kalra paragka...@gmail.com wrote: Sorry for the top post. I should have done bottom post. :( # Untested use strict; use warnings; open my $fh, '', my_file; while($fh){ if ($_ !~ /my_string/) { # Do something } } The other way would be on shell - # Untested grep my_string my_file if [ $? -eq 1 ] then echo Do something fi ~Parag On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
AW: Testing File Contents
The easiest way in my opinion is to use the 'grep' function like this: my $searchstring=whatever; open CFG, '', $_file || die(could not open file: $_file!); my @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) { print $searchstring found\n; } If you negate the grep like this: @data = grep !/$searchstring/i, @data; ... you can remove the searchstring from your array (file-text). best regards Christian Von: Matt [lm7...@gmail.com] Gesendet: Mittwoch, 2. März 2011 18:55 Bis: beginners@perl.org Betreff: Testing File Contents I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
# Untested use strict; use warnings; open my $fh, '', my_file; while($fh){ if ($_ !~ /my_string/) { # Do something } } This triggers on EVERY line of the file that does not contain the string. I just want to trigger once if its no where in the file. The other way would be on shell - # Untested grep my_string my_file if [ $? -eq 1 ] then echo Do something fi ~Parag On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
The easiest way in my opinion is to use the 'grep' function like this: my $searchstring=whatever; open CFG, '', $_file || die(could not open file: $_file!); my @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) { print $searchstring found\n; } This sorta worked. Needed a minor change. unless ( grep /$searchstring/i, @data ) { print $searchstring not found\n; Thanks. If you negate the grep like this: @data = grep !/$searchstring/i, @data; ... you can remove the searchstring from your array (file-text). I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On Wed, 2 Mar 2011, Matt wrote: The easiest way in my opinion is to use the 'grep' function like this: my $searchstring=whatever; open CFG, '', $_file || die(could not open file: $_file!); my @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) { print $searchstring found\n; } This sorta worked. Needed a minor change. unless ( grep /$searchstring/i, @data ) { print $searchstring not found\n; Thanks. My apologies if I'm beating a dead horse here, but I'm new to Perl and thought of a slightly different approach: my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp @ARGV or die qq/you didn't specify a filename\n/; open FH, q//, shift @ARGV or die qq/file open error: $!/; $_ = join q//, FH; close FH; if ( ! m/$searchrx/s ) { print qq/pattern not found\n/; # do something } I'm not sure which is more Perlish, but I hear TMTOWTDI is supposed to be valued, too. I welcome any criticism. Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: Testing File Contents
From: Brian F. Yulga [mailto:byu...@langly.dyndns.org] On Wed, 2 Mar 2011, Matt wrote: The easiest way in my opinion is to use the 'grep' function like this: my $searchstring=whatever; open CFG, '', $_file || die(could not open file: $_file!); my @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) { print $searchstring found\n; } This sorta worked. Needed a minor change. unless ( grep /$searchstring/i, @data ) { print $searchstring not found\n; Thanks. My apologies if I'm beating a dead horse here, but I'm new to Perl and thought of a slightly different approach: my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp This is probably personal preference, but I prefer to provide a more meaningful name rather than using @ARGV. Plus you have lost the filename once you 'shifted' @ARGV in the open statement. May want to use the file name in the open error statement or later. Also, I would not use the '/' as your quote delimiter - that's too recognizable as being used as the pattern match delimiter. Use actual double quotes () unless there are double quotes in the string - also less typing. Instead of '/' may want to use '{' and '}' if using qq (see perldoc perlop, Quote and Quote-like Operators). my $fileName = shift @ARGV or die You did not specify a filename\n; @ARGV or die qq/you didn't specify a filename\n/; Use lexical variable for filehandle. Makes things easier - such as passing to a function. open my $FH,'', $fileName or die Error opening $fileName: $!\n; open FH, q//, shift @ARGV or die qq/file open error: $!/; The join is unnecessary, set the input_record_separator ($/) to undef or use a local copy of $/ (see perldoc perlvar). If this is undefined, the entire file will be read into a variable. $_ = join q//, FH; close FH; if ( ! m/$searchrx/s ) { print qq/pattern not found\n/; # do something } I'm not sure which is more Perlish, but I hear TMTOWTDI is supposed to be valued, too. I welcome any criticism. Brian HTH, Ken -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
BFY == Brian F Yulga byu...@langly.dyndns.org writes: BFY My apologies if I'm beating a dead horse here, but I'm new to Perl and BFY thought of a slightly different approach: BFY my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp BFY @ARGV or die qq/you didn't specify a filename\n/; BFY open FH, q//, shift @ARGV or die qq/file open error: $!/; BFY $_ = join q//, FH; that is very slow and clunky. perl could slurp the file for you if you set $/ to undef. or better yet, use File::Slurp to do it BFY close FH; BFY if ( ! m/$searchrx/s ) { why are you using m//? the m isn't needed if you use // for delimiters the OP wants to know if a file has something or not, not to print each line. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
M == Matt lm7...@gmail.com writes: M I am looking for a simple way to test if a file does not contain a M string. This is on a linux box. M if myfile does not contain mystring { M #do_something; M } M The file is basically a list of names and I want to test that a M certain name is not in there. Is there an easy way to do that? 2 lines will do it: use File::Slurp ; unless( read_file( $file ) =~ /$whatever/ ) { # do something } faster and cleaner than the line by line methods others have posted. and if you want even more speed, and the file is long, then shell out to the grep utility and it has an option to only show you if a line is/isn't in file. i normally don't recommend shelling out but that would likely be the fastest solution for a large file. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com wrote: M == Matt lm7...@gmail.com writes: 2 lines will do it: use File::Slurp ; unless( read_file( $file ) =~ /$whatever/ ) { # do something } what's better about File::Slurp than just doing: my( $file, $string ) = @argv; open my $fh, '', $file; while( $fh ) { print found if /$string/ ; } ???
Re: Testing File Contents
sw == shawn wilson ag4ve...@gmail.com writes: sw On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com wrote: M == Matt lm7...@gmail.com writes: 2 lines will do it: use File::Slurp ; unless( read_file( $file ) =~ /$whatever/ ) { # do something } what's better about File::Slurp than just doing: sw my( $file, $string ) = @argv; sw open my $fh, '', $file; sw while( $fh ) { sw print found if /$string/ ; sw } less code, much much faster. you loop over each line. my code does one regex call and stays inside perl for that. inside perl is usually faster than running perl op. use the benchmark module and look at the difference. it will be noticeable. also your code looks at every line whereas my will match the first time and then quit. that is another optimization. you could do the same if you exited the loop upon a match. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On Mar 2, 2011 4:47 PM, Uri Guttman u...@stemsystems.com wrote: sw == shawn wilson ag4ve...@gmail.com writes: sw On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman u...@stemsystems.com wrote: M == Matt lm7...@gmail.com writes: 2 lines will do it: use File::Slurp ; unless( read_file( $file ) =~ /$whatever/ ) { # do something } what's better about File::Slurp than just doing: sw my( $file, $string ) = @argv; sw open my $fh, '', $file; sw while( $fh ) { sw print found if /$string/ ; sw } less code, much much faster. you loop over each line. my code does one regex call and stays inside perl for that. inside perl is usually faster than running perl op. use the benchmark module and look at the difference. it will be noticeable. How can I tell or do I figure out if code 'stays inside perl' or not? And, I'm not exactly sure what you mean by that either?
RE: Testing File Contents
-Original Message- From: Matt [mailto:lm7...@gmail.com] Sent: Wednesday, March 02, 2011 11:25 To: beginners@perl.org Subject: Re: Testing File Contents # Untested use strict; use warnings; open my $fh, '', my_file; while($fh){ if ($_ !~ /my_string/) { # Do something } } This triggers on EVERY line of the file that does not contain the string. I just want to trigger once if its no where in the file. Add a simple switch which is set to zero. Then look for the value you want. When found then you should be able to set to nonzero and leave the while. Then you should be able to check: if switch is not true, then send the email... If you have any questions and/or problems, please let me know. Thanks. Wags ;) David R. Wagner Senior Programmer Analyst FedEx Services 1.719.484.2097 Tel 1.719.484.2419 Fax 1.408.623.5963 Cell http://Fedex.com/us The other way would be on shell - # Untested grep my_string my_file if [ $? -eq 1 ] then echo Do something fi ~Parag On Wed, Mar 2, 2011 at 9:55 AM, Matt lm7...@gmail.com wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
sw == shawn wilson ag4ve...@gmail.com writes: less code, much much faster. you loop over each line. my code does one regex call and stays inside perl for that. inside perl is usually faster than running perl op. use the benchmark module and look at the difference. it will be noticeable. sw How can I tell or do I figure out if code 'stays inside perl' or not? And, sw I'm not exactly sure what you mean by that either? a regex will go inside perl's internals and run there. a perl loop as you wrote will execute perl operations. the perl interpreter is slow when executing operations (all perl ops, loop, syntax, etc). but once you get inside an operation it runs fast because the code is in c. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On 02/03/2011 17:55, Matt wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? Hey Matt If your file is small then this subroutine will do what you need. sub file_contains { my $file, $string = @_; open $file, '', $file or die $!; my %names = map { chomp; ($_ = 1) } DATA; return $names{$string}; } HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
RD == Rob Dixon rob.di...@gmx.com writes: RD On 02/03/2011 17:55, Matt wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? RD Hey Matt RD If your file is small then this subroutine will do what you need. RD sub file_contains { RD my $file, $string = @_; you forgot the () around the my vars. that will assign the count of @_ to $file. not what you want. perl -le '@foo = qw( a b ) ;my $x, $y = @foo ; print $x $y' 2 RD open $file, '', $file or die $!; RD my %names = map { chomp; ($_ = 1) } DATA; don't you mean $file there? RD return $names{$string}; RD } uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
On 02/03/2011 23:56, Uri Guttman wrote: RD == Rob Dixonrob.di...@gmx.com writes: RD On 02/03/2011 17:55, Matt wrote: I am looking for a simple way to test if a file does not contain a string. This is on a linux box. if myfile does not contain mystring { #do_something; } The file is basically a list of names and I want to test that a certain name is not in there. Is there an easy way to do that? RD Hey Matt RD If your file is small then this subroutine will do what you need. RDsub file_contains { RD my $file, $string = @_; you forgot the () around the my vars. that will assign the count of @_ to $file. not what you want. perl -le '@foo = qw( a b ) ;my $x, $y = @foo ; print $x $y' 2 RD open $file, '', $file or die $!; RD my %names = map { chomp; ($_ = 1) }DATA; don't you mean$file there? RD return $names{$string}; RD} Thanks Uri. It's midnight and I should be in bed :-/ Version 2: sub file_contains { my ($file, $string) = @_; open my $fh, '', $file or die $!; my %names = map { chomp; ($_ = 1) } $fh; return $names{$string}; } Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
RD == Rob Dixon rob.di...@gmx.com writes: RD Thanks Uri. It's midnight and I should be in bed :-/ we should all be in bed! RD Version 2: RD sub file_contains { RD my ($file, $string) = @_; RD open my $fh, '', $file or die $!; RD my %names = map { chomp; ($_ = 1) } $fh; RD return $names{$string}; RD } did you see my fast slurp version in this thread? uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
Ken Slater wrote: From: Brian F. Yulga [mailto:byu...@langly.dyndns.org] On Wed, 2 Mar 2011, Matt wrote: The easiest way in my opinion is to use the 'grep' function like this: my $searchstring=whatever; open CFG, '', $_file || die(could not open file: $_file!); my @data=CFG; close CFG; if ( grep /$searchstring/i, @data ) { print $searchstring found\n; } This sorta worked. Needed a minor change. unless ( grep /$searchstring/i, @data ) { print $searchstring not found\n; Thanks. My apologies if I'm beating a dead horse here, but I'm new to Perl and thought of a slightly different approach: my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp This is probably personal preference, but I prefer to provide a more meaningful name rather than using @ARGV. Plus you have lost the filename once you 'shifted' @ARGV in the open statement. May want to use the file name in the open error statement or later. I agree with you, saving the filename to a variable is probably a better practice, in general. In this context it wasn't specified that it was needed later, so I opted to just 'shift' it to use once. Also, I would not use the '/' as your quote delimiter - that's too recognizable as being used as the pattern match delimiter. Use actual double quotes () unless there are double quotes in the string - also less typing. Instead of '/' may want to use '{' and '}' if using qq (see perldoc perlop, Quote and Quote-like Operators). Good point. I started using generic quotes almost exclusively when I discovered that they avoided some of the possible shell-interpolation issues that arise when executing perl one-liners. I've probably taken it too far. I can see that '/' is not a great idea. Besides '{' and '}' as quote delimiters, do you think it's generally tolerated to use '(', ')', '[', ']' ? my $fileName = shift @ARGV or die You did not specify a filename\n; @ARGV or die qq/you didn't specify a filename\n/; Use lexical variable for filehandle. Makes things easier - such as passing to a function. open my $FH,'', $fileName or die Error opening $fileName: $!\n; open FH, q//, shift @ARGV or die qq/file open error: $!/; Okay, I'll work on breaking that habit. My first lessons in Perl used the open FH convention, and I just got used to it. The join is unnecessary, set the input_record_separator ($/) to undef or use a local copy of $/ (see perldoc perlvar). If this is undefined, the entire file will be read into a variable. Oh, I totally forgot about the input_record_separator ( $/ )... That's WAY better (actually when I wrote the 'join', I was thinking that I shouldn't need to do it that way!) Thanks much for the suggestions, Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Testing File Contents
Uri Guttman wrote: BFY == Brian F Yulga byu...@langly.dyndns.org writes: BFY My apologies if I'm beating a dead horse here, but I'm new to Perl and BFY thought of a slightly different approach: BFY my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp BFY @ARGV or die qq/you didn't specify a filename\n/; BFY open FH, q//, shift @ARGV or die qq/file open error: $!/; BFY $_ = join q//, FH; that is very slow and clunky. perl could slurp the file for you if you set $/ to undef. or better yet, use File::Slurp to do it Yes, definitely better to set $/ to undef. I'm still trying to get a handle on the libraries at my disposal -- There's so much in CPAN it's hard to know where to start. Is it equivalently efficient to use IO::All or IO::Simple, or is File::Slurp truly the best for this purpose? I ask because I played with IO:All to read files, enjoyed the simple, intuitive syntax my $filecontents io('filename'); , but (perhaps naively) assumed the native open my $fh to be faster since it doesn't require a use to load a library. BFY close FH; BFY if ( ! m/$searchrx/s ) { why are you using m//? the m isn't needed if you use // for delimiters Yeah, I know, I don't need it... being verbose (sometimes) keeps a newbie like me from making silly mistakes :-) My initial experimentation with Perl has been mostly in a line-by-line mind-set because I have a tendency to write scripts that are used like: some-unix-command | perl -wne 'do some processing' myresults.txt I'm trying to expand my horizons, thanks for help, Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/