|
From: | lina |
Subject: | Re: [Help-bash] how to translate one file |
Date: | Wed, 04 Jan 2012 13:39:39 +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 3In 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
#!/usr/bin/env bash declare -A dict while read line; do IFS=" " case $line in \"[A-Za-z]" "*) ## a=${line:20:4} # here I don't know how to read ## "A c #FFFFFF " /* "0" */, ## "B c #F8F8F8 " /* "0.0385" */, ## the digital part out? thanks ahead for your suggestions. dict[${line:1:1}]=${line:20:4} esac done < dm_1.xpmtail -n 1 dm_1.xpm | tr -d '"' ## Here I don't know how to pass the result -- the string, into a parameter called lastline. hope to get your advice.
#echo ${#lastline} lastline=AnnnnnnnnnnnnklmebUbQflnjnnlmeXSJKHJTXdhgnn for ((i=0; i<${#lastline}; i++)); do printf "%s " "${dict["${lastline:i:1}"]}" | tr -d '"' | tr -d "\*" done Best regards,
[Prev in Thread] | Current Thread | [Next in Thread] |