Have a look at this:
Code:
#!/bin/bash
shopt -s extglob
retail=( dummy retail foo bar )
brand=( dummy brandNo1 brandNo2 brandNo3 Alpine_starts )
class=( dummy classNo1 classNo2 Textile_Jacket )
style=( dummy 1 2 3 4 5 6 7 8 AST-1 )
colour=( dummy 1 2 3 4 5 6 7 8 9 10 11 12 13 WP )
for THISFILE in [0-9]*
do
RET=${THISFILE:0:1}
BRA=${THISFILE:1:3} ; BRA=${BRA##+(0)}
CLA=${THISFILE:4:2} ; CLA=${CLA##+(0)}
STY=${THISFILE:6:3} ; STY=${STY##+(0)}
COL=${THISFILE:9:2} ; COL=${COL##+(0)}
VER=${THISFILE:11:2}
echo "$THISFILE|${retail[RET]}|${brand[BRA]}|${class[CLA]}|${style[STY]}|${colour[COL]}|$VER" >> outfile
done
Example run:
Code:
$ ls -l
total 108
drwxr-x--- 2 druuna druuna 69632 aug 7 14:39 .
drwxr-x--- 7 druuna jd 32768 jul 15 18:08 ..
-rw-r----- 1 druuna druuna 0 aug 7 13:58 1004030091401
-rw-r----- 1 druuna druuna 0 aug 7 14:37 2003020070306
-rwxr-x--- 1 druuna druuna 607 aug 7 14:39 parser.sh
$ ./parser.sh
$ cat outfile
1004030091401|retail|Alpine_starts|Textile_Jacket|AST-1|WP|01
2003020070306|foo|brandNo3|classNo2|7|3|06
I don't know your level of expertise, so here's a breakdown:
This is needed to allow extended globbing, which is used later on (
${BRA##+(0)}).
Code:
retail=( dummy retail foo bar )
brand=( dummy brandNo1 brandNo2 brandNo3 Alpine_starts )
class=( dummy classNo1 classNo2 Textile_Jacket )
style=( dummy 1 2 3 4 5 6 7 8 AST-1 )
colour=( dummy 1 2 3 4 5 6 7 8 9 10 11 12 13 WP )
This creates the needed arrays. First entry is dummy, this to make sure you can count normally (arrays start counting with 0 instead of 1). All entries are separated by spaces.
Code:
for THISFILE in [0-9]*
do
.
.
done
This fetches the files that only contains numbers
Code:
RET=${THISFILE:0:1}
BRA=${THISFILE:1:3} ; BRA=${BRA##+(0)}
CLA=${THISFILE:4:2} ; CLA=${CLA##+(0)}
STY=${THISFILE:6:3} ; STY=${STY##+(0)}
COL=${THISFILE:9:2} ; COL=${COL##+(0)}
VER=${THISFILE:11:2}
This fills individual entries with the relevant parts from the file name and removes the possible leading zero(s). Both are done using bash' Parameter Expansion (do have a look at the bash manual page).
Code:
echo "$THISFILE|${retail[RET]}|${brand[BRA]}|${class[CLA]}|${style[STY]}|${colour[COL]}|$VER" >> outfile
Echo the associated parts by looking them up in the array and put them in a file called outfile.