LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Converting Data - Integer to string variable (https://www.linuxquestions.org/questions/linux-newbie-8/converting-data-integer-to-string-variable-4175472462/)

shanekelly 08-07-2013 06:53 AM

Converting Data - Integer to string variable
 
Hi Forum,

I need to write a script that will look at a file name with a 13 digit number and convert it to a human friendly name. The numbers need to be broken down in stages (is this called parsing?). For example 1004030091401 is broken down to 1 means retail then then next three digits 044 refer to the brand, the next two digits 03 refer to the product class, the next three digits 009 is the style the next two digits is the colour 14 and lastly the two remaining digits are which version it is, in this instance version 1 (for multiple photos of products etc). The desirable outcome of all of this particular example wouldbe a text file with 7 columns 1004030091401|Retail|Alpine_Stars|Textile_Jacket|AST-1 WP|01

I think I need to write an array to store all the variables I'll need. Then parse each file in turn and lastly output to a text file or similar? Any help would be greatly appreciated as I don't know where to start with this one. Thanks in anticipation.

grail 08-07-2013 07:51 AM

So you have told us how to break the number down, which is fairly easy in a multitude of languages (which you haven't said which one you want to use / try), but you have not explained
how the conversion of the numbers will be worked out, ie. how do we convert 1 into Retail?? Is this data stored elsewhere?

druuna 08-07-2013 07:52 AM

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:
Code:

shopt -s extglob
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.

shanekelly 08-09-2013 05:18 AM

Thanks very much Druuna, I'll try this later on and let you now how I got on.


All times are GMT -5. The time now is 12:50 AM.