使用shell脚本重命名文件名

使用shell脚本重命名文件名

我有很多文件名,如下所示;

KO.ADVT..HHE.D.2017.163.121959.SAC
KO.ADVT..HHN.D.2017.163.121957.SAC
KO.ADVT..HHZ.D.2017.163.121959.SAC
KO.ARMT..HHE.D.2017.163.121957.SAC
KO.ARMT..HHN.D.2017.163.121957.SAC
KO.ARMT..HHZ.D.2017.163.121956.SAC
KO.BGKT..HHE.D.2017.163.121958.SAC
KO.BGKT..HHN.D.2017.163.121959.SAC
KO.BGKT..HHZ.D.2017.163.121954.SAC
KO.BRGA..HNE.D.2017.163.121955.SAC
KO.BRGA..HNN.D.2017.163.121959.SAC
KO.BRGA..HNZ.D.2017.163.121959.SAC
KO.BUYA..HNE.D.2017.163.121954.SAC
KO.BUYA..HNN.D.2017.163.121955.SAC
KO.BUYA..HNZ.D.2017.163.121956.SAC
KO.CAVI..HHE.D.2017.163.121958.SAC
KO.CAVI..HHN.D.2017.163.121958.SAC
KO.CAVI..HHZ.D.2017.163.122001.SAC
KO.CRLT..HHE.D.2017.163.121958.SAC
KO.CRLT..HHN.D.2017.163.121959.SAC
KO.CRLT..HHZ.D.2017.163.121958.SAC
KO.CTYL..HHE.D.2017.163.122000.SAC
KO.CTYL..HHN.D.2017.163.121959.SAC
KO.CTYL..HHZ.D.2017.163.122004.SAC
KO.DST..HNE.D.2017.163.121959.SAC
KO.DST..HNN.D.2017.163.121957.SAC
KO.DST..HNZ.D.2017.163.121956.SAC
KO.EDRB..HHE.D.2017.163.121959.SAC
KO.EDRB..HHN.D.2017.163.121955.SAC
KO.EDRB..HHZ.D.2017.163.121958.SAC

我想像这样改变他们的名字

ADVT.HHE.KO
ADVT.HHN.KO
ADVT.HHZ.KO
ARMT.HHE.KO
ARMT.HHN.KO
ARMT.HHZ.KO
BGKT.HHE.KO
BGKT.HHN.KO
BGKT.HHZ.KO
BRGA.HNE.KO
BRGA.HNN.KO
BRGA.HNZ.KO
BUYA.HNE.KO
BUYA.HNN.KO
BUYA.HNZ.KO
CAVI.HHE.KO
CAVI.HHN.KO
CAVI.HHZ.KO
CRLT.HHE.KO
CRLT.HHN.KO
CRLT.HHZ.KO
CTYL.HHE.KO
CTYL.HHN.KO
CTYL.HHZ.KO
DST.HNE.KO
DST.HNN.KO
DST.HNZ.KO
EDRB.HHE.KO
EDRB.HHN.KO
EDRB.HHZ.KO

我用了这个代码

 for file in *.SAC
    do
        newfilename="${file:3:6}${file:7:9}"
    echo mv $file $newfilename
    done

但结果是这样的;

ADVT....HHE.D.2
ADVT....HHN.D.2
ADVT....HHZ.D.2
DST..H.HNE.D.20
DST..H.HNN.D.20
DST..H.HNZ.D.20
.... so on.

如何使用我的代码获取新文件名?

提前致谢。

答案1

rename

rename -n 's/^(.*?)\.(.*?)\.\.(.*?)\..*/$2.$3.$1/' *SAC

-n 如果输出看起来不错,请拆下开关。

.*?就像.*但是不贪心

答案2

使用

for f in *SAC; do
    echo mv "$f" "$(awk -F. -v OFS=. '{print $2, $4, $1}' <<< "$f")"
done

echo如果输出看起来不错,请删除语句

相关内容