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!

FILE LOG ERROR REPORT 1

Status
Not open for further replies.

kcire

Programmer
May 15, 2009
18
BR
hello


i´d like to know the script that allows me to search in a file.txt a specific word or line, and report log errors by missing specific informations as words itself or datas.

 
What have you tried? What are you having problems with?
 


that´s a code that I have not known yet

im learning by books and i needed a little tip about this specific subject with someone,
 
Students are not allowed to post on tek-tips.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
could someone give any tip ?



give some light about it


 
could someone give any tip?


some light about it
 
Start here
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
can someone help me ?

to get a specific word, from a text.txt

here is the code,

#!C:/perl/bin/perl.exe
open (my $read, "c:/perl/notice.txt" )|| die "failed to open notice.txt $!\n";
while(<$read>) {
print "$_";
}
close $read;

print "search a word\?\n";
$choose=<STDIN>; chomp $choose;
if($choose =~ /[yes|ok]/) {
print "digit a word\n"};
$choose=<STDIN>; chomp $choose;
foreach(grep(/$choose/,my $read)) {
print "result:my $read\n";
}
obs: this is a veery simple code, i didnt complete the conditions as you can see , my focus is to make the perl get a word from the text.txt and display it on prompt as i asked
 
That's a good start...

[ul][li]A file handle is not a variable, so I have replaced $read with FILE where appropriate.[/li]
[li]You were printing the contents of the input file, but not actually storing it anywhere to search it later. I have stored it in the array @read.[/li]
[li]I changed "digit" to "enter" because I think that's what you really meant.[/li][/ul]

Code:
#!C:/perl/bin/perl.exe

open FILE, "c:/perl/notice.txt" || die "failed to open notice.txt $!\n";
@read=<FILE>;   # read entire file into array
close FILE;
chomp @read;

print "search a word\?\n";
$choose=<STDIN>; chomp $choose;
if($choose =~ /yes|ok/) {
        print "enter a word\n";
        $choose=<STDIN>; chomp $choose;
        foreach(grep(/$choose/,@read)) {
                print "result: $_\n";
        }
}

Annihilannic.
 
A file handle is not a variable, so I have replaced $read with FILE where appropriate.

A lexical filehandle is fine and is sort of the standard these days in perl.

Code:
open( my $file,  "foo");
while(<$file>){
   ....
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Mmm... interesting, thanks! I re-read man perlopentut and see they are described as "Indirect Filehandles", I'll keep that in mind.

Annihilannic.
 
a last question guys

how to make a condition to the file notice.txt that if the word was not found it gives a msg "sry this word was not found"

here´s the code once again

#!C:/perl/bin/perl.exe
start:
open FILE, "c:/perl/notice.txt" || die "failed to open notice.txt $!\n";
@read=<FILE>; # read entire file into array
close FILE;
chomp @read;

print "Search a word\n";
$choose=<STDIN>; chomp $choose;
if($choose =~ /yes/){print "enter a word\n";
$choose=<STDIN>; chomp $choose;
foreach(grep(/$choose/,@read)) {
print "result: $_\n";

}
}

else{
print "invalid\n";
goto start;

}
 
Create a variable, say $found, set it to 0 before the search.. if a match is found set it to 1... and then after the loop say something like if ($found) print "sorry".

Annihilannic.
 
nice, Annihilannic.

where can i place the value=1 when the key word match with the content of .txt ?


im trying set a variable to the txt file and refering to it print "sry\n" if it doesnt match with de txt content, but doesnt work





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top