index, find regex

Discussion in 'Perl Misc' started by Ben Dover, Sep 12, 2003.

  1. Ben Dover

    Ben Dover Guest

    i have some .jpg files created by a digital camera (nikon 5700).
    the camera imbeds time and date in the binary.

    viewing the binary file, i can see the numeric dates
    "2003:09:10 21:55:01"
    that number is the date/time.
    i was able to do a regex to find it:
    /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/

    but then I had trouble finding it's position with index. it returns a
    "-1" on every occurance.
    here's the code:


    #!/usr/bin/perl

    # variables
    $picFile='DSCN0155.JPG';

    open (READ, $picFile);
    while ($line=<READ>){
    if ($line=~ /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/) {
    $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");
    $substrA=substr($line,$indexA,400);
    print $indexA;
    }
    }
    close (READ);
     
    Ben Dover, Sep 12, 2003
    #1
    1. Advertising

  2. if you just want to read the date you can put the regex in brackets
    and the date will be stored in $1 -


    $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    print $1;
    # --- or ----
    ($date) = $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    print $date;

    if you need the position, add 'g' to the end of regex and use the pos
    function
    (the pos function returns where the match ended so take off the length of
    the date
    to get the start position)

    ($date) = $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/g;
    print pos($line) - length($date);

    --

    Matt

    "Ben Dover" <> wrote in message
    news:...
    > i have some .jpg files created by a digital camera (nikon 5700).
    > the camera imbeds time and date in the binary.
    >
    > viewing the binary file, i can see the numeric dates
    > "2003:09:10 21:55:01"
    > that number is the date/time.
    > i was able to do a regex to find it:
    > /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/
    >
    > but then I had trouble finding it's position with index. it returns a
    > "-1" on every occurance.
    > here's the code:
    >
    >
    > #!/usr/bin/perl
    >
    > # variables
    > $picFile='DSCN0155.JPG';
    >
    > open (READ, $picFile);
    > while ($line=<READ>){
    > if ($line=~ /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/) {
    > $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");
    > $substrA=substr($line,$indexA,400);
    > print $indexA;
    > }
    > }
    > close (READ);
     
    Matt Churchyard, Sep 12, 2003
    #2
    1. Advertising

  3. "Matt Churchyard" <> wrote in message
    news:...
    > if you just want to read the date you can put the regex in brackets
    > and the date will be stored in $1 -
    >
    >
    > $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    > print $1;
    > # --- or ----
    > ($date) = $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    > print $date;
    >
    > if you need the position, add 'g' to the end of regex and use the pos
    > function
    > (the pos function returns where the match ended so take off the length of
    > the date
    > to get the start position)
    >
    > ($date) = $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/g;
    > print pos($line) - length($date);
    >


    after realising i hadn't tested that last piece of code I ran it
    and have realised that for some intrieging reason, the /g modifier
    does not work when you try to gather the results using the '($date) ='
    syntax. (atleast not on my winxp/activestate perl5.8 pc)
    Therefore, the code above must be written

    $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/g;
    print pos($line) - length($1);

    > --
    >
    > Matt
    >
    > "Ben Dover" <> wrote in message
    > news:...
    > > i have some .jpg files created by a digital camera (nikon 5700).
    > > the camera imbeds time and date in the binary.
    > >
    > > viewing the binary file, i can see the numeric dates
    > > "2003:09:10 21:55:01"
    > > that number is the date/time.
    > > i was able to do a regex to find it:
    > > /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/
    > >
    > > but then I had trouble finding it's position with index. it returns a
    > > "-1" on every occurance.
    > > here's the code:
    > >
    > >
    > > #!/usr/bin/perl
    > >
    > > # variables
    > > $picFile='DSCN0155.JPG';
    > >
    > > open (READ, $picFile);
    > > while ($line=<READ>){
    > > if ($line=~ /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/) {
    > > $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");
    > > $substrA=substr($line,$indexA,400);
    > > print $indexA;
    > > }
    > > }
    > > close (READ);

    >
    >
     
    Matt Churchyard, Sep 12, 2003
    #3
  4. Ben Dover

    Ben Dover Guest

    i also realized that this binanry jpg file also has occurances of the
    date time twice in one line.
    like:


    Matt Churchyard wrote:
    >
    > if you just want to read the date you can put the regex in brackets
    > and the date will be stored in $1 -
    >
    > $line =~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    > print $1;
    > # --- or ----

    .......
     
    Ben Dover, Sep 12, 2003
    #4
  5. Ben Dover

    Tore Aursand Guest

    On Fri, 12 Sep 2003 08:39:36 -0400, Ben Dover wrote:
    > #!/usr/bin/perl


    use strict;
    use warnings;

    > # variables
    > $picFile='DSCN0155.JPG';
    >
    > open (READ, $picFile);
    > while ($line=<READ>){
    > if ($line=~ /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/) {
    > $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");
    > $substrA=substr($line,$indexA,400);
    > print $indexA;
    > }
    > }
    > close (READ);


    Very un-Perl to do it this way. :) Try this one instead;

    while ( <READ> ) {
    if ( m,(\d{4}:\d{2}:\d{2}) (\d{2}:\d{2}:\d{2}), ) {
    my $date = $1;
    my $time = $2;

    print "$date $time\n";
    }
    }

    You get the point. Look at those parantheses.


    --
    Tore Aursand <>

    "Yes, madam, I am drunk. But in the morning I will be sober and you will
    still be ugly." -- Winston Churchill, replying to Lady Astor's comment
    "Sir, you're drunk!"
     
    Tore Aursand, Sep 12, 2003
    #5
  6. Ben Dover

    ko Guest

    Ben Dover wrote:
    > i have some .jpg files created by a digital camera (nikon 5700).
    > the camera imbeds time and date in the binary.
    >
    > viewing the binary file, i can see the numeric dates
    > "2003:09:10 21:55:01"
    > that number is the date/time.
    > i was able to do a regex to find it:
    > /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/
    >
    > but then I had trouble finding it's position with index. it returns a
    > "-1" on every occurance.
    > here's the code:


    You have to use index either:
    1. index STR,SUBSTR,POSITION
    or:
    2. index STR,SUBSTR

    index() returns -1 when the substring does *not* match. The reason it
    didn't match was:

    > $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");

    => HERE

    'SUBSTR' is a literal string, not a regex. So you are literally trying
    to match '\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d'. If you want to use
    index(), save the match from your regex:

    $line=~ /(\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d)/;
    $indexA = index($line, $1);

    Please read the posting guidleines:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    Information on how to use Perl built-in functions is available on your
    system:

    perldoc -f index

    HTH - keith
     
    ko, Sep 12, 2003
    #6
  7. Ben Dover

    Tulan W. Hu Guest

    "Ben Dover" <> wrote in...
    > #!/usr/bin/perl
    >
    > # variables
    > $picFile='DSCN0155.JPG';
    >
    > open (READ, $picFile);
    > while ($line=<READ>){
    > if ($line=~ /\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d/) {
    > # $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");

    $dateString = $line;
    $dateString =~ s/(.*)(\d{4}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2})(.*)/$2/;
    $indexA = index($line, $dateString);
    > $substrA=substr($line,$indexA,400);
    > print $indexA;
    > }
    > }
    > close (READ);
     
    Tulan W. Hu, Sep 12, 2003
    #7
  8. Ben Dover

    Ben Dover Guest

    ok!
    here's version .00000000000001 of my tiny project. its more dynamic now.
    it takes input of file names like anyother un*x program from the command
    line.
    then it reads each file and adds the date / time to the filename like
    this:

    file pic01.jpg has embedded date of 9/11/2001 time of 08:47:01
    file pic02.jpg has embedded date of 9/11/2002 time of 09:05:01
    file pic03.jpg has embedded date of 9/11/2003 time of 10:05:01

    they become this
    pic01_2001-09-11_08_47_01.jpg
    pic02_2002-09-11_09_05_01.jpg
    pic03_2003-09-11_10_05_01.jpg

    simple enough.


    #!/usr/bin/perl

    # variables



    foreach $picFile (@ARGV) {
    open (READ, $picFile);
    ENDHERE: while ($line=<READ>){
    if ($line=~ /\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}/) {
    $line =~ /(\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2})/;
    $datetime=$1;
    last ENDHERE;
    }
    }
    close (READ);

    $datetime=~ s|(\d{4}):(\d{2}):(\d{2})
    (\d{2}):(\d{2}):(\d{2})|_$1-$2-$3_$4_$5_$6|;


    $dotPos=rindex($picFile, '.');
    $filename=substr($picFile, 0,$dotPos);
    $fileExt=substr($picFile, $dotPos+1, length($picFile)-$dotPos-1);

    $renTo= "$filename$datetime.$fileExt";

    #testing!!! later this procedure will rename files
    print "$picFile ==> $renTo\n";
    }



    (yeah,yeah, use strict, warnings, its just a test)
     
    Ben Dover, Sep 12, 2003
    #8
  9. Ben Dover

    Ben Dover Guest

    ok! ok.
    here's version .00000000000002 of my tiny project.
    what do you think?

    #!/usr/bin/perl

    # variables
    $setFileExt='.JPG'; #camera produces this extension

    foreach $picFile (@ARGV) {
    #if it does not end in $setFileExt, skip file.
    $findExt=rindex($picFile,$setFileExt);
    if ($findExt eq "-1"){
    next;
    }

    open (READ, $picFile);
    ENDHERE: while ($line=<READ>){
    if ($line=~ /\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}/) {
    $line =~ /(\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2})/;
    $datetime=$1;
    last ENDHERE;
    }
    }
    close (READ);

    $datetime=~ s|(\d{4}):(\d{2}):(\d{2})
    (\d{2}):(\d{2}):(\d{2})|_$1-$2-$3_$4_$5_$6|;


    $dotPos=rindex($picFile, '.');
    $filename=substr($picFile, 0,$dotPos);
    $fileExt=substr($picFile, $dotPos+1, length($picFile)-$dotPos-1);

    $renTo= "$filename$datetime.$fileExt";


    # print "$picFile ==> $renTo\n"; #testing purposes
    rename $picFile, $renTo || warn "could not mv $picFile ==> $renTo
    $!";
    }
     
    Ben Dover, Sep 12, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    712
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. karthikeyavenkat
    Replies:
    2
    Views:
    597
    Bryce
    Mar 17, 2005
  3. Replies:
    3
    Views:
    769
    Reedick, Andrew
    Jul 1, 2008
  4. Shawn W_
    Replies:
    5
    Views:
    284
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    298
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page