Solved

how to open a file and Search for a string

Posted on 2009-05-19
7
303 Views
Last Modified: 2012-05-07
i have a file with some text , when i open and search for some string i am getting errors as below

No such class FILE at work.pl line 6, near "open my FIL
syntax error at work.pl line 6, near "my FILE,"
Execution of work.pl aborted due to compilation errors.

my file contains text as below

hihih
didj lmd
dnkndkd
dnkondll
bkdfkdfkndkfsnf DCLIENT: lost connection to BE
ndknkdsnsd

and below is my code, can u please tell me where the problem is


$koti = "kot.txt";
open my FILE,'>', $koti or die "'$koti' file will not open $!";
while (<TEST>) {
  if ($_ =~ m/DCLIENT: lost connection to BE/g) {
    print "Connection Lost"
  }
}

Open in new window

0
Comment
Question by:koti688
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:Romi Kuntsman
ID: 24420141
Does it work if you put $file instead of FILE?
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 24420151
FILE is a file handle, not a perl variable. Try this

$koti = "kot.txt";
open FILE,">$koti" or die "'$koti  file will not open $!";
while (<FILE>) {
  if ($_ =~ m/DCLIENT: lost connection to BE/g) {
    print "Connection Lost"
  }
}
close(FILE);
0
 
LVL 7

Expert Comment

by:thiyaguk
ID: 24420388
open IN, "myfile.csv" or die "cannot open";
while(<IN>)
{
 $flag=1 if(/mystring/);
 print if($flag);
}

or

if unix, you can use sed/awk

sed -n '/pattern/,$p' file
 
awk '/pattern/{f=1}f' file




0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:FishMonger
ID: 24421275
>> FILE is a file handle, not a perl variable. Try this

Actually, it's neither.   At this point it's only a bareword.

romi_k was nudging koti688 in the right direction.  Since perl 5.6 it's preferable to use a lexical var for the filehandle instead of a bareword and the 3 arg form of open.

open my $FILE, '>', $koti or die "'$koti' file will not open $!";

Open in new window

0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 500 total points
ID: 24431460
There are two issue with the code segment you have pasted:

1. Using my for FILE , my is not required here. That is giving the error.
2. The other one is a programing error, you are declaring the file-handle as FILE and using TEST in while loop to read data.

So these two line should look like this:

open FILE,'>', $koti or die "'$koti' file will not open $!";
while (<FILE>) {

0
 
LVL 6

Expert Comment

by:zlobcho
ID: 24646757
Try this:
#!/usr/bin/perl

open (FILE,"kot.txt") or die "kot.txt file will not open $!";
while (<FILE>) {
  if ($_=~m/DCLIENT: lost connection to BE/) {
    print "Connection Lost";
  }
}
  close FILE;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question