?
Solved

How to read data in known format from a file

Posted on 2004-09-14
8
Medium Priority
?
163 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:sunnybrad
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 12060513
while( <> ){
    print "$1\n" if /^([^:]):/;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 12060516
while( <> ){
    print "$1\n" if /^([^:]+):/;
}
0
 
LVL 5

Expert Comment

by:jpfx
ID: 12067785
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:jpfx
ID: 12067890
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
 

Author Comment

by:sunnybrad
ID: 12070084
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
 

Author Comment

by:sunnybrad
ID: 12070527
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
 
LVL 1

Expert Comment

by:JustinPincar
ID: 12071831
How about:

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

This looks for a '/' followed by as few of anything as it can followed by a ':'.
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 12071850
print "$1\n" if /([^\/:]+):/;
0

Featured Post

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!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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
Suggested Courses

752 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