Bach Script

Dear all,
I am bad with bash programming and I need some help how I can make variable names out of a string.

For example I have a lot of filenames (strings) following the same convention

m02_+1+7_London_0000$01.cfg
m02_+1+8_London_0000$01.cfg
m02_+1+8_London_0000$01.cfg
m02_+2+7_London_0000$01.cfg
m02_+2+7_London_0000$01.cfg
m02_+1+7_London_0000$01.cfg

What I want to do is to create a script that will interpret the following string and save into variables part of its name
m02_+1+7_London_0000$01.cfg as
------X-Y–City---------

X=1
Y=7
City=London

after that I want the script to
mkdir City (this should create a folder called London)
mkdir City/M’X.Y’ (this should create a subfolder called London/M1.7

then I want to copy the files that go all the files with the same City and X and Y to the same subfolder
City/MX.Y

I will need some help start doing that. And I think the first would be to get part of the filenames strings into variables.

I would like to thank you in advance for your help

BR
Alex

Well normally I program Mozart scripts, but I’ll give this a bash (groan).

Look into the substring extraction and regular expression features of bash.

E.g. if $i contains foo2bar3, then ${i:3:1} has the value 2.

You mean something like this:

#!/bin/bash
#
# Set base for source dir
SOURCEDIR="."
# Set base for destination dir
BASEDIR="."

FILES=$SOURCEDIR/m02_*.cfg
for FILE in $FILES ; do
        F=$(basename $FILE)
        echo $F
        X=$(echo $F | cut -d '+' -f 2)
        Y=$(echo $F | cut -d '+' -f 3 | sed -e "s/_.*//")
        CITY=$(echo $F | cut -d '_' -f 3)

        if ! test -d $BASEDIR/$CITY/M$X.$Y ; then
                mkdir -p $BASEDIR/$CITY/M$X.$Y
        fi

        cp -a $FILE $BASEDIR/$CITY/M$X.$Y/
done
exit 0

Dear alaios,

I see that you get plenty of help here in spite of the fact that you did not ask rthis in the subforum with the name Programming/Scripting under Developement.
Next time please consider asking your scripting questions there.

you are divine!

I am sorry I have missed that! My bad

On 07/13/2011 02:36 PM, ken yap wrote:
>
> Well normally I program Mozart scripts

i only looked in because i so enjoy most of Johann Sebastian’s scripts
(though they are usually called ‘works’ and not ‘scripts’).


DD
Caveat-Hardware-Software

openSUSE®, the BMW® of operating systems!

On 2011-07-13 15:36, hcvv wrote:
>
> Dear alaios,
>
> I see that you get plenty of help here in spite of the fact that you
> did not ask rthis in the subforum with the name Programming/Scripting
> under Developement.
> Next time please consider asking your scripting questions there.

IMHO, it should be moved, lest it gets long.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)