views:

56

answers:

3

I have already made a regex which should work, but it doesn't work.

echo "FileName.17:09:2010 4.16.PM.720p.mp4" | sed -E 's/\d{2}:\d{2}:\d{4}\ (\d|\d{2})\.(\d{2}|\d)\.((AM)|(PM))//g'

Should output: FileName..720p.mp4

But instead outputs the same "FileName.17:09:2010 4.16.PM.720p.mp4".

+1  A: 

Is \d a valid character class in sed? Try replacing it with [0-9] or [:digit:]. See re_format.

Sean Fausett
Also, you could simplify your regex by removing unnecessary alternates to `sed -E 's/[0-9]+:[0-9]+:[0-9]+ [0-9]+\.[0-9]+\.(AM|PM)//g'`.
Sean Fausett
Yeah thought about that when I made the regex but thought it would do a better match if I done it with the exact number of numbers. Though it is allot neater.
Mint
A: 
bash$ echo "FileName.17:09:2010 4.16.PM.720p.mp4"|ruby -e 's=gets.chomp.split(".");puts [s[0],s[-2,2]].join(".")'
FileName.720p.mp4
+1  A: 
#! /bin/sh
f="FileName.17:09:2010 4.16.PM.720p.mp4"
echo ${f%%.*}${f##*[AP]M}

This works for any variable containing a string matching the pattern. See Parameter Expansion for shell variables.

With sed, just delete from the first dot to the AM or PM. Or, if the filename could have extraneous dots, then delete from the 1st number folowed by ':' up to [AP]M, 's/\.[0-9]\+:.*\[AP]M//'

I think the sed way might be better; because if it fails it returns the original string. A mismatch on the shell expression would return some of the name twice; but error checking can be added easily: It fails for files not having AM or PM in the name.

echo FileName.17:09:2010 4.16.PM.720p.mp4|sed  -e 's/\..*[AP]M\././'
Frayser