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
Solved

How to read data in known format from a file

Posted on 2004-09-14
8
160 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search in text file in column and compare 4 53
ppm conversion to curl on a module install 8 86
Regular Expression for URL 10 102
Perl script to process a .csv file 18 44
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

860 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