On Wed, Jan 04, 2012 at 01:39:39PM +0800, lina wrote:
"A c #FFFFFF " /* "0" */,
"B c #F8F8F8 " /* "0.0385" */,
AnnnnnnnnnnnnklmebUbQflnjnnlmeXSJKHJTXdhgnn
OK, is *this* the final input file? Are we sure now? We're not going to
change it again?
And you want the output to be what? "FFFFFF121212121212...." without
the # signs?
#!/usr/bin/env bash
declare -A dict
while read -r key _ value _; do
[[ $key = \"* ]] || break
key=${key#\"}
value=${value#\#}
dict["$key"]=$value
done
n=${#key}
for ((i=0; i<n; i++)); do
printf %s "${dict["${key:i:1}"]}"
done
echo
And here's my input file to test:
imadev:~$ cat foo
"A c #FFFFFF " /* "0" */,
"B c #F8F8F8 " /* "0.0385" */,
ABBA
And here's the script running:
imadev:~$ ./bar< foo
FFFFFFF8F8F8F8F8F8FFFFFF
If that isn't what you want, then the fault is yours for not providing a
*clear* definition of the input and the desired output.
Oh, by the way, let's jump ahead to what I can only imagine will be the
next change in the problem specification:
Subject: Re: [Help-bash] how to translate one file
You said "one file". But in many of your non-working code examples, you
appear to be attempting to translate multiple files in some kind of loop.
So let's assume that you actually meant "I have many *.xpm files in a given
directory and I want to perform this translation on each of them, one at
a time, with all the outputs being written together to stdout."
(That's probably not exactly what you want either, but tough. Deal with
it. I'm getting sick of changing specifications.)
In that case, just wrap the translator in a function, so you can call it
repeatedly:
#!/usr/bin/env bash
translate() {
declare -A dict
local key value n i
while read -r key _ value _; do
[[ $key = \"* ]] || break
key=${key#\"}
value=${value#\#}
dict["$key"]=$value
done
n=${#key}
for ((i=0; i<n; i++)); do
printf %s "${dict["${key:i:1}"]}"
done
echo
}
for f in ./*.xpm; do
translate< "$f"
done