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: Tue, 03 Jan 2012 23:15:20 +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 Tuesday 03,January,2012 09:17 PM, Greg Wooledge wrote:
On Mon, Jan 02, 2012 at 11:46:08PM +0800, lina wrote:
I have one file like

a 1
b 2
c 3

and the last line of the file like :

abc

I wish to translate it to
1 2 3
In bash 4, you can do this elegantly using an associative array:

#!/usr/bin/env bash
declare -A dict
while read key value; do
     # If there is no second field, then we've just read the final line.
     [[ $value ]] || break
     # Otherwise, enter this pair into our dictionary.
     dict["$key"]=$value
done

# Last line has already been read, and is in "key"
n=${#key}
for ((i=0; i<n; i++)); do
     printf "%s " "${dict["${key:i:1}"]}"
done
echo
Thanks, very crystal clear.
When I derive it to a field problem,

$ cat dm_1.xpm | while read line ; do IFS=$'\n' ; if [ ${#line} == 30 ]; then echo $line ; fi ; done


/* Generated by g_mmdat_g_f */
/* legend:  "Distance (nm)" */
/* x-label: "Residue Index" */
/* y-label: "Residue Index" */
static char *grmacos_xpm[] = {
"D  c #EBEBEB " /* "0.115" */,
"E  c #E5E5E5 " /* "0.154" */,
"F  c #DEDEDE " /* "0.192" */,
"G  c #D8D8D8 " /* "0.231" */,
"H  c #D1D1D1 " /* "0.269" */,
"I  c #CBCBCB " /* "0.308" */,
"J  c #C4C4C4 " /* "0.346" */,
"K  c #BEBEBE " /* "0.385" */,
"L  c #B7B7B7 " /* "0.423" */,
"M  c #B1B1B1 " /* "0.462" */,
"O  c #A3A3A3 " /* "0.538" */,
"P  c #9D9D9D " /* "0.577" */,
"Q  c #969696 " /* "0.615" */,
"R  c #909090 " /* "0.654" */,
"S  c #898989 " /* "0.692" */,
"T  c #838383 " /* "0.731" */,
"U  c #7C7C7C " /* "0.769" */,
"V  c #767676 " /* "0.808" */,
"W  c #6F6F6F " /* "0.846" */,
"X  c #696969 " /* "0.885" */,
"Y  c #626262 " /* "0.923" */,
"Z  c #5C5C5C " /* "0.962" */,

I have problem in dropping the first 5 lines. I wish to start with the parts include key and value.

if I use:
$ cat dm_1.xpm | while read line ; do IFS=$'\n' ; if [ ${#line} == 30 -a ${line:0:1}== '"' ]; then echo $line ; fi ; done
bash: [: too many arguments

Thanks,






reply via email to

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