how to open a file and Search for a string

Posted on 2009-05-19
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 line 6, near "open my FIL
syntax error at line 6, near "my FILE,"
Execution of aborted due to compilation errors.

my file contains text as below

didj lmd
bkdfkdfkndkfsnf DCLIENT: lost connection to BE

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

Question by:koti688

Expert Comment

by:Romi Kuntsman
ID: 24420141
Does it work if you put $file instead of FILE?
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"

Expert Comment

ID: 24420388
open IN, "myfile.csv" or die "cannot open";
 $flag=1 if(/mystring/);
 print if($flag);


if unix, you can use sed/awk

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

LVL 28

Expert Comment

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


Accepted Solution

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>) {


Expert Comment

ID: 24646757
Try this:

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;

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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