2

I'm very new to Bash scripts and would like to test it out on the following problem I have. I currently have a lot of files in a directory in the form

V3_August_'day'_0_'simulations'.pickle

where 'day' and 'simulations' represent variables that change for different files. I would like to convert these into the form

V3_2012_8_'day+1'_0_'simulations'.pickle

What would a script that iterates through these files, renaming them as required be?

4
  • I'm not sure, but I don't think that a bash script would necessarily be the best idea/easy/possible (OK, it would probably be possible, but probably also ugly). Perhaps you should try using Perl. Commented Aug 5, 2013 at 18:32
  • 1
    Ah ok, for some reason I thought Bash was good for dealing with these things. I wrote something to do it in Python instead. Commented Aug 5, 2013 at 18:40
  • Could you also post some sample file names? Commented Aug 5, 2013 at 18:46
  • Sample fileNames would be V3_August_1_0_2000.pickle or V3_August_5_0_400.pickle to V3_2012_8_2_0_2000.pickle and V3_2012_8_6_0_400.pickle Commented Aug 5, 2013 at 19:03

1 Answer 1

3

Pure bash based solution:

for x in *.pickle; do
   [[ $x =~ ^([^_]+_[^_]+_)([^_]+)(_.+)$ ]] && mv "$x" \
     "${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))${BASH_REMATCH[3]}"
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.