想要把IMG_20160618_144009_DF3.jpg,中的_DF3这块去掉。
在man中看到 rename ‘y/A-Z/a-z/’ * 用来替换大小写,以为正则必须用 y,
用以下命令不成功:
rename 'y/_[A-Z0-9]{3}\././' *
后来百度了下才知道 有 s/ y/的区别。
记录下来,惠人惠己。
二、Perl正则表达式
2.1 三种形式[2]
匹配:m/<regexp>/ (可以省略m,直接写成/regexp/)
替换:s/<pattern>/<replacement>/
转化:tr/<pattern>/<replacement>/
博文[3]给出了8种模式,rename命令常用到替换和转化两种(用匹配也没意义)。转换跟替换不同,替换是将replacement整个字符串替换pattern字符串,而转换则是用replacement逐个字符替换pattern逐个字符,结果依赖于replacement与pattern字符个数(见下述分析)。(1)替换
替换表达方式如下[3],还有一系列参数,貌似不怎么用得着。
s/PATTERN/REPLACEMENT/egimosx
eEvaluate the right side as an expression. gMatch globally, i.e. all occurrences. iCase-insensitive pattern matching. mTreat string as multiple lines. oOnly compile pattern once, even if variables within it change. sTreat string as single line. xUse extended regular expressions
(2)转化
转化有两种等价表达方式,如下:
tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds
cComplement the SEARCHLIST. dDelete found but unreplaced characters. sSquash duplicate replaced characters.
转化同替换不同,用REPLACEMENTLIST逐个字符替换SEARCHLIST逐个字符,比如’tr/Sam/Stm/’,用S替代S,t替代a,m替代m。结果依赖于两者字符长短,下面以文件名FastSpiSam3C.nc为例进行说明:
jelline@jelline:~$ rename -n 'tr/Sam3/Stm/' FastSpiSam3C.nc /*替换字符短,用最后一个字符m替换3*/
FastSpiSam3C.nc renamed as FtstSpiStmmC.nc
jelline@jelline:~$ rename -n 'tr/Sam3/Stm32/' FastSpiSam3C.nc /*替换字符长,多出字符被忽略*/
FastSpiSam3C.nc renamed as FtstSpiStm3C.nc