开发者

Find and replace date/time in string

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

echo "FileName.17:09:2010 4.16.PM.开发者_如何学JAVA720p.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".


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


#! /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\././'


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜