You are not logged in.

#1 2010-04-05 15:27:17

ogronom
Member
From: Toronto, Canada
Registered: 2008-05-06
Posts: 123

[SOLVED]sed: regex repeated pattern

Hi all,

Currently I'm struggling with bash script for automatic presorting and converting of photos from camera.
While man page says that {} is repeated of previous pattern it does not work for me. Here is the example (basically I want extract year of the timestamp).

% exiv2 20080930--DCIM/100CANON/IMG_0002.JPG| grep timestamp
Image timestamp : 2008:05:09 10:25:02
% exiv2 20080930--DCIM/100CANON/IMG_0002.JPG| grep timestamp | sed 's@.*\([0-9]{4}\).*@\1@'
Image timestamp : 2008:05:09 10:25:02
% exiv2 20080930--DCIM/100CANON/IMG_0002.JPG| grep timestamp | sed 's@.*\([0-9]{3}\).*@\1@'
Image timestamp : 2008:05:09 10:25:02

And this one works

% exiv2 20080930--DCIM/100CANON/IMG_0002.JPG| grep timestamp | sed 's@.*\([0-9][0-9][0-9][0-9]\).*@\1@'
2008

If I need repeated m times this is not acceptable. How to deal with this?

Last edited by ogronom (2010-04-05 16:26:54)

Offline

#2 2010-04-05 15:53:21

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]sed: regex repeated pattern

' exiv2 20080930--DCIM/100CANON/IMG_0002.JPG| grep timestamp | cut -c 19-22' might do as well.

Offline

#3 2010-04-05 16:20:29

ogronom
Member
From: Toronto, Canada
Registered: 2008-05-06
Posts: 123

Re: [SOLVED]sed: regex repeated pattern

I'm not interested in workarounds (there is one posted above). I'm wondering why  {} does not work. Most porobable my understanding of the man re_syntax is incorrect, I just wonder why.

Offline

#4 2010-04-05 16:23:58

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]sed: regex repeated pattern

Escaping '{' and '}' helps:

sed 's@.*\([0-9]\{4\}\).*@\1@'

Last edited by karol (2010-04-05 16:24:22)

Offline

#5 2010-04-05 16:26:32

ogronom
Member
From: Toronto, Canada
Registered: 2008-05-06
Posts: 123

Re: [SOLVED]sed: regex repeated pattern

Found
Silly issue. instead of {} one should use \{\}

Offline

#6 2010-04-05 16:27:27

ogronom
Member
From: Toronto, Canada
Registered: 2008-05-06
Posts: 123

Re: [SOLVED]sed: regex repeated pattern

Thanks, karol

Offline

Board footer

Powered by FluxBB