Ask a Question related to PERL Miscellaneous, Design and Development.
-
Ben Dover #1
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
-
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... -
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? -
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... -
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... -
where can find index service module (unix)
i wnat to find index service like ms index service. where can find these document or module?? -
Matt Churchyard #2
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
-
Matt Churchyard #3
Re: index, find regex
"Matt Churchyard" <matt@userve.net> wrote in message
news:3f61bd85@news.userve.net...after realising i hadn't tested that last piece of code I ran it> 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);
>
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
-
Ben Dover #4
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
-
Tore Aursand #5
Re: index, find regex
On Fri, 12 Sep 2003 08:39:36 -0400, Ben Dover wrote:
use strict;> #!/usr/bin/perl
use warnings;
Very un-Perl to do it this way. :-) Try this one instead;> # 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);
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
-
ko #6
Re: index, find regex
Ben Dover wrote:
You have to use index either:> 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:
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:
=> HERE> $indexA=index ($line, "\d\d\d\d:\d\d:\d\d \d\d:\d\d:\d\d");
'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
-
Tulan W. Hu #7
Re: index, find regex
"Ben Dover" <Ben_Dover@pickupsoap.forme.com> wrote in...
$dateString = $line;> #!/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 =~ 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
-
Ben Dover #8
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
-
Ben Dover #9
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



Reply With Quote

