Solved

How to read data in known format from a file

Posted on 2004-09-14
8
156 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 500 total points
ID: 12071850
print "$1\n" if /([^\/:]+):/;
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now