By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,248 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,248 IT Pros & Developers. It's quick & easy.

Trouble getting data from a file

P: 5
Hi all,

i hope you can help, I'm having some trouble getting all the data from a file, well just the date and time entries. I'll be doing operations on the data so would like to store it within variables.

My script's output is looking a bit strange...

The file is sent from a server so changing it will be difficult. Nearly all of the data are in quotes except the date and time.

First line of the file are the headings, then the data:
"Car ID" "Description" "Status" "Condition" "Time arrived" "Location" "car_counrty"
"ABC123" "Lamborghini Countach" "used" "good" 17/02/2006 15:00:00 "alpha" "Italy"

My script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. print "enter filename\n";
  5. chomp(my $filename = <>);
  6.  
  7. open(FILE, $filename) || die "can't open $!\n";
  8.  
  9. while (<FILE>){
  10.   if (m/ABC/){
  11.   my ($ID, $description, $status, $condition, $time_arr, $location, $country) = split/(\"\w+\")/;
  12.   print "ID:$ID\ndescription: $description\nstatus: $status\ncondition: $condition\narrvied: $time_arr\nlocation: $location\ncountry: $country\n\n";
  13.   }
  14. }
  15.  

and the output:
ID:
description: "ABC123"
status: "Lamborghini Countach"
condition: "used"
arrvied:
location: "good"
country: 17/02/2006 15:00:00

How would i get the date and time? Any assistance would be greatly apprieciated.

i know the issue lies on the 11th line, but this has annoyed me for a bit now :(

thanks for yuor time
g0uki
Feb 16 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
This suggestion is very dependent on the file being formatted exactly how you showed and that the script does not have to validate lines for proper formatting:

Expand|Select|Wrap|Line Numbers
  1. while (<FILE>){
  2.   if (m/ABC/){
  3.   my ($ID, $description, $status, $condition, $time_arr, $location, $country) = split/"\s+"|"\s+|\s+"/;
  4.   $ID =~ s/^"//;
  5.   print "ID:$ID\ndescription: $description\nstatus: $status\ncondition: $condition\narrvied: $time_arr\nlocation: $location\ncountry: $country\n\n";
  6.   }
  7. }
  8.  
Feb 16 '08 #2

P: 5
Nice!
Thanks KevinADC :)

I dont understand how you've made it work though?

the line: split/"\s+"|"\s+|\s+"/;

splits the line by whitespace, but 2 times?

"\s+|\s+"/; -> this tests for an 'or' option?

Would it be okay if its explained? i really want to get to grasp with perl's regex.

Thanks again
g0uki
Feb 16 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
the '|' used in that context literally means "or" (called alternation)

split/"\s+" or "\s+ or \s+"/;

first it tries to split on 'comma space(s) comma' : "\s+"
then it tries to split on 'comma space(s)' : "\s+
and last it tries to split on 'space(s) comma ': \s+"

those are the three patterns in the lines that will nearly result in the fields of data you want, all that remains to do is remove the leading comma from the first field which is what the next line does:

$ID =~ s/^"//;

there are other ways to split those lines into fields of data, that was just the first way I saw that would work using split(). If the fields are fixed width you could use substr or unpack, but if they are not fixed width fields you can use split or other regexps.
Feb 16 '08 #4

P: 5
Hi KevinADC,

Sorry for my late reply and thanks for the explanation into this problem i was having.

I like the way its been explained, but will admit that so far in my perl training i haven't come across regex alternation yet, but it looks very handy.

thanks again for your help on this
best regards
g0uki
Feb 19 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
You're welcome. I noticed I kept saying "comma" in my explanation when I should have said "double-quote".
Feb 19 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.