2

I have an issue about how to change orders of that sac files name. I mean I have this order but I need to change it to the second one and thes ecommands should be on the script. Additionally, this change must be done with using column numbers.

OLD

2007.084.00.22.50.8930_2007.084.01.26.10.1430.GE.ISP..BHZ.D.SAC
2007.084.00.22.51.2930_2007.084.01.26.54.8430.GE.ISP..BHN.D.SAC
2007.084.00.25.42.9430_2007.084.01.27.59.2430.GE.ISP..BHE.D.SAC

NEW

2007.084.01.26.10.1430_2007.084.00.22.50.8930.GE.ISP..BHZ.D.SAC
2007.084.01.26.54.8430_2007.084.00.22.51.2930.GE.ISP..BHN.D.SAC
2007.084.01.27.59.2430_2007.084.00.25.42.9430.GE.ISP..BHE.D.SAC

Let me write the code that I try to finish in this way (if the answers will be in that way, it will be more helpful for me) and I don't know how to continue to finish this process.

#!/bin/bash

for sacfile in *SAC
do

echo $sacfile
n2=`echo $sacfile | cut -c 24-45`
echo $n2

done 

1 Answer 1

2

Assuming they're fixed length filenames this should work.

#!/bin/bash

for sacfile in *SAC
do
    new_sacfile=${sacfile:23:22}_${sacfile:0:22}${sacfile:45}
    echo mv -v ${sacfile} ${new_sacfile}
done 

We're using bash's built in variable manipulation to cut up the file name and rearrange it to our desire. When you're happy you can remove the echo in front of the mv statement

4
  • Thanks for your commend but I only get mv -v 2007.084.00.22.50.8930_2007.084.01.26.10.1430.GE.ISP..BHZ.D.SAC 2007.084.01.26.10.1430_2007.084.00.22.50.8930.GE.ISP..BHZ.D.SAC mv -v 2007.084.00.22.51.2930_2007.084.01.26.54.8430.GE.ISP..BHN.D.SAC 2007.084.01.26.54.8430_2007.084.00.22.51.2930.GE.ISP..BHN.D.SAC mv -v 2007.084.00.25.42.9430_2007.084.01.27.59.2430.GE.ISP..BHE.D.SAC 2007.084.01.27.59.2430_2007.084.00.25.42.9430.GE.ISP..BHE.D.SAC on my terminal screen, so I couldn't get these files with new names ?
    – kejinatsu
    Oct 31, 2016 at 16:03
  • Well like I said in the post "When you're happy with the output you can remove the echo from in front of the mv statement" Oct 31, 2016 at 16:04
  • oh, sorry mate I unnoticed that, it works great Thank you so much.
    – kejinatsu
    Oct 31, 2016 at 16:09
  • @kejinatsu not a problem, happy to help. Oct 31, 2016 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .