2

I have lots of file name like below;

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

and I want to change their name like this

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

I used this code

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

but the result like this;

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.

How can I get new files names with my code ?

Thanks in advance.

2 Answers 2

2

With rename:

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

Remove -n switch if the output looks good.

.*? is like .* but not greedy

1
  • 1
    @Gilles Quenot it works like a charm :) thanks a lot !!!
    – curious
    Commented Jun 17, 2020 at 13:15
1

Using and :

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

Remove echo statement if the output looks good

5
  • 1
    Quenoti this is also works, thank you so much !!!
    – curious
    Commented Jun 17, 2020 at 13:15
  • This is less elegant than perl's one, up2u (IMO) Commented Jun 17, 2020 at 13:17
  • I have never used perl but I wonder about it. Is there any suggestion how can I learn perl coding ?
    – curious
    Commented Jun 17, 2020 at 13:19
  • Sure, find an assignment of treating text/xml/JSON... Any data, and start learning. Perl is amazing when you need to treat data. The regex engine is the best/most advanced one of the high level languages out there. I'm a fan of perl one-liners, you can do many things with just one line of condensed line. Commented Jun 17, 2020 at 13:24
  • thank you so much @Gilles Quenot again !
    – curious
    Commented Jun 18, 2020 at 12:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.