LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-07-2013, 07:53 AM   #1
shanekelly
LQ Newbie
 
Registered: Jun 2011
Posts: 11

Rep: Reputation: Disabled
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.
 
Old 08-07-2013, 08:51 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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?
 
Old 08-07-2013, 08:52 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
1 members found this post helpful.
Old 08-09-2013, 06:18 AM   #4
shanekelly
LQ Newbie
 
Registered: Jun 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks very much Druuna, I'll try this later on and let you now how I got on.
 
  


Reply

Tags
array, integer, parse, string, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Shell Script - Store a variable as a string not an integer RML1992 Linux - General 8 09-12-2012 10:19 AM
converting integer data to string in fortran 90 s_hy Programming 1 03-13-2012 04:53 PM
Converting string to integer in BASH pgb205 Programming 17 08-31-2010 09:38 PM
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 06:59 PM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 06:27 PM


All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration