0

I'm just learning perl and I'm trying to learn regular expressions at the same time. Basically I'm trying to open a log file and print out any lines that match user input to a new file. Using the following code I get no output at all if I type in the word "Clinton". But if I replace

print MYFILE if /\$string\;

with

print MYFILE if /\Clinton\; 

it runs as expected. Any ideas? I know it is something simple that I am missing.

print "Enter a word to look up: ";
$string = <>;
print "You put $string";
open(LOG,"u_ex121011.log") or die "Unable to open logfile:$!\n";
open (MYFILE, '>>data2.txt');
while(<LOG>){   
   print MYFILE if /\Q($string)\E/;
}
close (MYFILE); 
close(LOG);
print "Check data2.txt";

3 Answers 3

4

In Perl, unlike in some languages, the input operator doesn't silently remove a trailing newline. So your $string is actually "Clinton\n" rather than than "Clinton". To fix it, use the chomp function:

$string = <>;
chomp $string;
print "You put $string\n";
Sign up to request clarification or add additional context in comments.

6 Comments

Ah perfect. I combined all 3 answers and it works now. Thanks guys!
You're welcome! But for the record, squiguy's answer is wrong: the $_ =~ is optional. (I mean, (s)he wrote "should", which I guess makes it a matter of opinion, but personally I disagree.) And I think you should keep the \Q and \E: as (s)he says, you only need it if there's a chance the user will input special characters -- which is always. So you should keep it.
Exactly why is the $_ optional? Sorry I'm still new to perl.
I have the following now and it's working pretty well. Any revisions that I should make before I move forwards? print "Enter a word to look up: "; $string = <>; chomp $string; print "You put $string"; open(LOG,"u_ex121011.log") or die "Unable to open logfile:$!\n"; open (MYFILE, '>>data_excluding_json.txt'); while(<LOG>){ my @lines = split( ' ', $_ ); if (/\Q$string\E/ && !/\json\b/i){ print MYFILE "$lines[0] $lines[1] $lines[4] $lines[7] \n"; } } close (MYFILE); close(LOG); print "Check data_excluding_json.txt";
Re: "Exactly why is the $_ optional?": It's because $_ is the "default variable"; various operators and functions, such as m// and s/// and chomp and split, operate on it unless they're specifically given something else to operate on instead.
|
0

You should also use the 3 argument version of open.

open( my $LOG, '<', 'u_ex121011.log' ) or die "Unable to open file:$!\n";

open http://perldoc.perl.org/functions/open.html

1 Comment

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
-1

In addition to what ruakh said, you should check if the string is on the line by using the $_ variable and the =~ operator.

print MYFILE "$_\n" if $_ =~ /\Q$string\E/;

Going off of your comment, you can split the line up surprisingly enough using split. Here is an example of what you could do:

my @lines = split( ' ', $_ );
print MYFILE "$lines[0] $lines[1] $lines[2] $lines[3]\n";

Here is documentation of split: http://perldoc.perl.org/functions/split.html

3 Comments

I have another question building on this. My log files have columns separated by a space. I know that I only want everything before the 4th space that occurs and don't want anything else. How would I do that?
-1 for the bad advice to remove the \Q\E modifiers. Since $string contains user input, you should use the \Q\E modifiers to escape any metacharacters unless you purposefully want the user input to be interpreted as a regular expression instead of a string literal.
@SamChoukri Well it's all preference based on what he needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.