How to read data in known format from a file

Dear All:

I have a question regarding reading the file contents. The file contents are of the form
/test/test2/test3/test4/myfile.h:5://Comments//
There are multiple entries in this format. Comments can spill over to the next line. There are entries which are not in this format but first I want to process entries in just in this format. I am only interested in
/test/test2/test3/test4/myfile.h.


I want to extract this in a variable. Please show me how through code fragment.

Best Regards

sunnybrad
sunnybradAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
print "$1\n" if /([^\/:]+):/;
0
 
ozoCommented:
while( <> ){
    print "$1\n" if /^([^:]):/;
}
0
 
ozoCommented:
while( <> ){
    print "$1\n" if /^([^:]+):/;
}
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
jpfxCommented:
another way... (written for windows and I'm open to better ways. I'm offering this only as an alternative)

#!perl.exe -w
my ($a,$b,$c,$d,$e)="";
# change 'file' to filename
open (IN,"file") or die "cannot open file: $!";
while (<IN>) {
    chomp;
      $_=~s/^\///; # remove first '/'
      if (/\x2f\x2f/){
            (undef,$e)=split(/\/\//,$_);
            print "line $. $e\n";
      } else {
            ($a,$b,$c,$d)=split(/\//,$_);
      }
      print "a=$a b=$b c=$c d=$d\n";
}
close (IN) or die "cannot close file: $!";

This way you have the data in variables a-e which you can do what you want with.
0
 
jpfxCommented:
oops, you should reset the variables at the end of the loop in that code snippet and the print statements need fixing a bit but hopefully you get the idea.
0
 
sunnybradAuthor Commented:
Dear Ozo:

I am not quite that good with regular expressions yet could you please give some explaination with the solution.

Best Regards

sunnybrad
0
 
sunnybradAuthor Commented:
Dear Ozo:

I have an entry like

 /test/test1/test2/test3/test4/test.H: 10:       Copyright (c) 1989-1995 My, Inc. All Rights Reserved.

The second regular expression is not getting me  /test/test1/test2/test3/test4/test.H. From this I want to only get test.H.

Please help.

Regards

sunnybrad
0
 
JustinPincarCommented:
How about:

while( <> ){
    print "$1\n" if /\/(.*?):/;
}

This looks for a '/' followed by as few of anything as it can followed by a ':'.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.