index, find regex

B

Ben Dover

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);
 
M

Matt Churchyard

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);
 
M

Matt Churchyard

Matt Churchyard said:
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);
 
B

Ben Dover

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


Matt said:
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 ----
.......
 
T

Tore Aursand

#!/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 <[email protected]>

"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!"
 
K

ko

Ben said:
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
 
T

Tulan W. Hu

Ben Dover said:
#!/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);
 
B

Ben Dover

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)
 
B

Ben Dover

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
$!";
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top