[SWLUG] regexp and sed

bascule asura at theexcession.co.uk
Tue Dec 24 09:41:56 UTC 2002


i've been knocking up a script to rename files as mentioned earlier on this 
list, thanks to various folk i can now take the files off my camera and name 
them according to their timestamps, i couldn't use jhead because not all the 
camera files are pics there are audio files as well, this has meant that i 
had to keep the file extension as well and extract this from the original 
filename, looking at sed after dafydd mentioned it and dipping into various 
descriptions of regexps has led to this current incarnation:
 for i in `ls` ;
	do mv $i `stat -c "%y" $i | sed -e "s/\(.*\)\ 
\(.*\):\(.*\):\(.*\)\..*/\1_\2.\3.\4/"``basename $i | sed -e 
's/.*\(\..*\)/\1/g'`;
	done

wordwrapping will have obscurred this a bit but my question relates to the 
second sed statement:
sed -e 's/.*\(\..*\)/\1/g'
running the entire script on a directory of files results in the following:
2002-12-12_22.12.40.jpg*  2002-12-12_22.13.26.jpg*
2002-12-12_22.13.02.jpg*  2002-12-17_19.38.50.jpg*

running the script again creates exactly the same file names, but this time 
the 'originals' have more than one period in them, as i understand it the 
regexp:
.*\(\..*\)
could be described as:
a sequence of any charcters followed by one period followed by a sequence of 
any characters - some of which may be periods,
it looks to me like each filename can be split in three different ways and 
still match this description, so - at last the question - where in the 
regexp, or in sed, is the logic that determines that the part:
\(\..*\)
only matches the last period and what follows and not any of the other periods 
and what follows?

if this has been clearer than mud then hooray! if not then feel free to throw 
some back!

bascule

-- 
I CAN BE ROBBED BUT NEVER DENIED, I TOLD MYSELF. WHY WORRY?
'I too cannot be cheated,' snapped Fate.
SO I HAVE HEARD.
(Colour of Magic)





More information about the Swlug mailing list