help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to translate one file


From: lina
Subject: Re: [Help-bash] how to translate one file
Date: Wed, 04 Jan 2012 22:51:41 +0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16

On Wednesday 04,January,2012 10:14 PM, Greg Wooledge wrote:
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?
the output to be:
0 ....   ...     0.0385
numerical value, not #FFFFFF

Just a quick clarify, I am going to read your code carely.

Thanks,
#!/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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]