0

I use csplit to write separate smaller files from a single larger file

#!/bin/bash
OBJECT=$1; $TARGET="bigfile.pgn";

FIRST_NEW_GAME=$2; 

csplit $TARGET /Event/ {*};

ls xx*

echo 'How many games do we have?'; read NUMGAMES 

#xx00  xx01  xx02  xx03  xx04  xx05
# I just grab "5" for $NUMGAMES

I now have the 5 files and am ready to rename them to $FIRST_NEW_GAME+1

while [ $X -le $NUMGAMES ]; do
let X=X+1 
pseudo code: mv xx[01-05] 5337+[$X].pgn
done;

I have xx[01-01] and want to create FIRST_NEW_GAME ("5436") + $X For example: xx01 becomes 5336.pgn, xx02 becomes 5337.pgn, etc.

But that last while loop is beyond my scope. Any ideas?

2
  • csplit has options --suffix-format & --prefix. See if those will help you. You might not need any additional bash code at all... Commented Aug 18, 2015 at 4:27
  • Doesn't apply in this case. Commented Aug 18, 2015 at 6:44

1 Answer 1

1

Your pseudocode and examples use a different offset, just choose one and fill in. In the calculation (( newnum = num + offset )) the shell knows it is a calculation with variables, and you do need $ for the value of the vars.

offset=5337
for file in xx[0-9]*; do
        # Special syntax to cut of the xx, this is faster than
        # num=$(cut -dx -f3)
        # Using cut is easier to remember ;)
        num0=${file##*x}
        #num0 can start with a 0, thats octal. Remove the 0 in the front.
        num=${num0#0}
       (( newnum = num + offset ))
        mv $file  ${newnum}.pgn
done

EDIT: Tested with xx01 until xx05, but I forgot that 05 is 5 in octal notation. For 05 this is no problem, for 08 and 09 it is!

Sign up to request clarification or add additional context in comments.

4 Comments

Very nice! Only problem is the first xx00 has to be skipped and the for loop process xx01 and onwards. How can that best be done? OH, silly me! Just rm it, EZ as pie.
When you don't want to delete that file, only skip, you can add if [ "${file}" = "xx00" ]; then continue; fi
Busted. Darn. I tried it with 10 games and got these two error messages: /home/rmack/bin/prepare_bulk: line 28: ((: 08: value too great for base (error token is "08") /home/rmack/bin/prepare_bulk: line 28: ((: 09: value too great for base (error token is "09")
Sorry, I tested for 6 files and missed the xx08/xx09 problems: 08 is 8 in the octal notation, which makes no sense. See my edit for a solution.

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.