Link to home
Start Free TrialLog in
Avatar of sunnybrad
sunnybrad

asked on

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
Avatar of ozo
ozo
Flag of United States of America image

while( <> ){
    print "$1\n" if /^([^:]):/;
}
while( <> ){
    print "$1\n" if /^([^:]+):/;
}
Avatar of jpfx
jpfx

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.
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.
Avatar of sunnybrad

ASKER

Dear Ozo:

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

Best Regards

sunnybrad
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
How about:

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

This looks for a '/' followed by as few of anything as it can followed by a ':'.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial