Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default index, find regex

    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 Guest

  2. Similar Questions and Discussions

    1. setUpComplexFindReplace() does not find regex strings
      I wrote an extension to do multiple Find & Replaces by calling setUpComplexFindReplace() and replaceAll() and so far everything works fine, as long...
    2. How do I find the index of a DG column with a specific name?
      I have a column called "notes" which may change position in the datagrid. How do I find the index of this column based on its name?
    3. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
      Hey Folks,(New to .NET) This is driving me NUTZ... If anyone out there can resolve this from me I would greatly appreciate it... Line 238: Line...
    4. Cannot Find Row Index after using row filter
      I have a datagrid that works great and edits/updates just fine. So I decided to add rowfilter via a textbox search. This works great, however...
    5. where can find index service module (unix)
      i wnat to find index service like ms index service. where can find these document or module??
  3. #2

    Default Re: index, find regex

    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" <Ben_Dover@pickupsoap.forme.com> wrote in message
    news:3F61BE88.2794624F@pickupsoap.forme.com...
    > 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 Guest

  4. #3

    Default Re: index, find regex


    "Matt Churchyard" <matt@userve.net> wrote in message
    news:3f61bd85@news.userve.net...
    > 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" <Ben_Dover@pickupsoap.forme.com> wrote in message
    > news:3F61BE88.2794624F@pickupsoap.forme.com...
    > > 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 Guest

  5. #4

    Default Re: index, find regex

    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 Guest

  6. #5

    Default Re: index, find regex

    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 <tore@extend.no>

    "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 Guest

  7. #6

    Default Re: index, find regex

    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:
    [url]http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html[/url]

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

    perldoc -f index

    HTH - keith

    ko Guest

  8. #7

    Default Re: index, find regex

    "Ben Dover" <Ben_Dover@pickupsoap.forme.com> 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 Guest

  9. #8

    Default Re: index, find regex

    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 Guest

  10. #9

    Default Re: index, find regex



    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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139