<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Creating associative or hash arrays in bash using sed and strings without the use of arrays, looping and conditionals.</title>
	<atom:link href="http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/</link>
	<description>Microcomputers - Development - Systems</description>
	<pubDate>Tue, 07 Feb 2012 13:27:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Arun Sangal</title>
		<link>http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/#comment-22600</link>
		<dc:creator>Arun Sangal</dc:creator>
		<pubDate>Fri, 15 Apr 2011 21:11:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.microdevsys.com/WordPress/?p=995#comment-22600</guid>
		<description>Scripts logic for hashv and hashk funcations in the following lines is wrong in the sense that it'll not capture all index/values if an index1 string is contained in another index2 string or if value1 string is contained in some other value2 string.

The following line 
   echo $mh&#124;sed -e "s/.*\([ \t]*\)\($hkey\):\([^ \t]*\?\)\([ \t]*\).*/\3/gi"

in hashv() should be changed to:
echo $mhash &#124; sed "s/ /\n/g"&#124; grep "^${hkey}:" &#124;  cut -d':' -f2


AND

The following line 
echo $mh&#124;grep "$hvalue"&#124;sed -e "s/\([^ \t]*\)[:]\($hvalue\)/\&#124;\1&#124;\2\&#124;/i" -e "s/.*\?[&#124;]\(.*\)[&#124;].*[&#124;].*\?/\1/i";

in hashk () should be changed to:
echo $mhash &#124; sed "s/ /\n/g"&#124; grep ":${hvalue}$" &#124; cut -d':' -f1

Then, with any set of index:value pair i.e. a:1 or aa:11 or ab:2 abab:23 or cd:2323 will work correctly.</description>
		<content:encoded><![CDATA[<p>Scripts logic for hashv and hashk funcations in the following lines is wrong in the sense that it&#8217;ll not capture all index/values if an index1 string is contained in another index2 string or if value1 string is contained in some other value2 string.</p>
<p>The following line<br />
   echo $mh|sed -e &#8220;s/.*\([ \t]*\)\($hkey\):\([^ \t]*\?\)\([ \t]*\).*/\3/gi&#8221;</p>
<p>in hashv() should be changed to:<br />
echo $mhash | sed &#8220;s/ /\n/g&#8221;| grep &#8220;^${hkey}:&#8221; |  cut -d&#8217;:&#8217; -f2</p>
<p>AND</p>
<p>The following line<br />
echo $mh|grep &#8220;$hvalue&#8221;|sed -e &#8220;s/\([^ \t]*\)[:]\($hvalue\)/\|\1|\2\|/i&#8221; -e &#8220;s/.*\?[|]\(.*\)[|].*[|].*\?/\1/i&#8221;;</p>
<p>in hashk () should be changed to:<br />
echo $mhash | sed &#8220;s/ /\n/g&#8221;| grep &#8220;:${hvalue}$&#8221; | cut -d&#8217;:&#8217; -f1</p>
<p>Then, with any set of index:value pair i.e. a:1 or aa:11 or ab:2 abab:23 or cd:2323 will work correctly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arun K Sangal</title>
		<link>http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/#comment-22594</link>
		<dc:creator>Arun K Sangal</dc:creator>
		<pubDate>Fri, 15 Apr 2011 20:24:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.microdevsys.com/WordPress/?p=995#comment-22594</guid>
		<description>OK..corrected the logic for hash array. Now the below script will run like a 1969 pony ass. It's showing "55" now for abcd index ...and also not worrying about "eabcd" index (as this index contains "abcd" in the string). See the comment in script for "^" and "$" characters usage in grep.


[user@BS001 stage]$ cat ./gigahash.sh 
#!/bin/bash

mhash="abcd:55 bcda:4422 cdab:321 dabc:2131 eabcd:11 giga:aks";

function hashv {
        hkey="";
        mh="";
        if [[ $2 != "" ]]; then hkey=$2; else echo ""; return 0; fi
        if [[ $1 != "" ]]; then mh=$1; else echo ""; return 0; fi

        ## Note the placement of "^" character in grep.
        echo $mh &#124; sed "s/ /\n/g"&#124; grep "^${hkey}" &#124;  cut -d':' -f2
}

function hashk {
        hvalue="";
        mh="";
        if [[ $2 != "" ]]; then hvalue=$2; else echo ""; return 0; fi
        if [[ $1 != "" ]]; then mh=$1; else echo ""; return 0; fi

        ## Note the placement of "$" character in grep.
        echo $mh &#124; sed "s/ /\n/g"&#124; grep ${hvalue}$&#124; cut -d':' -f1
}

echo "____________________________________";
hashv "$mhash" "eabcd";
hashk "$mhash" "11";
echo "____________________________________";
hashv "$mhash" "";
hashk "" "11";
echo "____________________________________";
hashv "$mhash" "bcda";
hashk "$mhash" "4422";
echo "____________________________________";
hashv "$mhash" "abcd";
hashk "$mhash" "2131";
echo "____________________________________";
hashv "$mhash" "cdab";
hashk "$mhash" "55";
echo "____________________________________";

[user@BS001 stage]$ ./gigahash.sh
____________________________________
11
eabcd
____________________________________


____________________________________
4422
bcda
____________________________________
55
dabc
____________________________________
321
abcd
____________________________________
[user@BS001 stage]$</description>
		<content:encoded><![CDATA[<p>OK..corrected the logic for hash array. Now the below script will run like a 1969 pony ass. It&#8217;s showing &#8220;55&#8243; now for abcd index &#8230;and also not worrying about &#8220;eabcd&#8221; index (as this index contains &#8220;abcd&#8221; in the string). See the comment in script for &#8220;^&#8221; and &#8220;$&#8221; characters usage in grep.</p>
<p>[user@BS001 stage]$ cat ./gigahash.sh<br />
#!/bin/bash</p>
<p>mhash=&#8221;abcd:55 bcda:4422 cdab:321 dabc:2131 eabcd:11 giga:aks&#8221;;</p>
<p>function hashv {<br />
        hkey=&#8221;";<br />
        mh=&#8221;";<br />
        if [[ $2 != "" ]]; then hkey=$2; else echo &#8220;&#8221;; return 0; fi<br />
        if [[ $1 != "" ]]; then mh=$1; else echo &#8220;&#8221;; return 0; fi</p>
<p>        ## Note the placement of &#8220;^&#8221; character in grep.<br />
        echo $mh | sed &#8220;s/ /\n/g&#8221;| grep &#8220;^${hkey}&#8221; |  cut -d&#8217;:&#8217; -f2<br />
}</p>
<p>function hashk {<br />
        hvalue=&#8221;";<br />
        mh=&#8221;";<br />
        if [[ $2 != "" ]]; then hvalue=$2; else echo &#8220;&#8221;; return 0; fi<br />
        if [[ $1 != "" ]]; then mh=$1; else echo &#8220;&#8221;; return 0; fi</p>
<p>        ## Note the placement of &#8220;$&#8221; character in grep.<br />
        echo $mh | sed &#8220;s/ /\n/g&#8221;| grep ${hvalue}$| cut -d&#8217;:&#8217; -f1<br />
}</p>
<p>echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;eabcd&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;11&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;&#8221;;<br />
hashk &#8220;&#8221; &#8220;11&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;bcda&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;4422&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;abcd&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;2131&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;cdab&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;55&#8243;;<br />
echo &#8220;____________________________________&#8221;;</p>
<p>[user@BS001 stage]$ ./gigahash.sh<br />
____________________________________<br />
11<br />
eabcd<br />
____________________________________</p>
<p>____________________________________<br />
4422<br />
bcda<br />
____________________________________<br />
55<br />
dabc<br />
____________________________________<br />
321<br />
abcd<br />
____________________________________<br />
[user@BS001 stage]$</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arun Sangal</title>
		<link>http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/#comment-22587</link>
		<dc:creator>Arun Sangal</dc:creator>
		<pubDate>Fri, 15 Apr 2011 19:44:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.microdevsys.com/WordPress/?p=995#comment-22587</guid>
		<description>The above didn't work for the following example:

[user@BS001 stage]$ cat gigahash.sh
#!/bin/bash

mhash="abcd:55 bcda:4422 cdab:321 dabc:2131 eabcd:11";

function hashv {
        hkey="";
        mh="";
        if [[ $2 != "" ]]; then hkey=$2; else echo ""; return 0; fi
        if [[ $1 != "" ]]; then mh=$1; else echo ""; return 0; fi

        echo $mh&#124;sed -e "s/.*\([ \t]*\)\($hkey\):\([^ \t]*\?\)\([ \t]*\).*/\3/gi"
}

function hashk {
        hvalue="";
        mh="";
        if [[ $2 != "" ]]; then hvalue=$2; else echo ""; return 0; fi
        if [[ $1 != "" ]]; then mh=$1; else echo ""; return 0; fi

        echo $mh&#124;grep "$hvalue"&#124;sed -e "s/\([^ \t]*\)[:]\($hvalue\)/\&#124;\1&#124;\2\&#124;/i" -e "s/.*\?[&#124;]\(.*\)[&#124;].*[&#124;].*\?/\1/i";
}

echo "____________________________________";
hashv "$mhash" "eabcd";
hashk "$mhash" "11";
echo "____________________________________";
hashv "$mhash" "";
hashk "" "11";
echo "____________________________________";
hashv "$mhash" "bcda";
hashk "$mhash" "4422";
echo "____________________________________";
hashv "$mhash" "abcd";
hashk "$mhash" "2131";
echo "____________________________________";
hashv "$mhash" "cdab";
hashk "$mhash" "55";
echo "____________________________________";

[user@BS001 stage]$

Now when I run the above script, "11" in 
"11 
dabc
" is wrong. i.e. "hashv "$mhash" "abcd";" failed to print the correct value of 55.. it printed 11 (which is for eabcd hash key's value). Looks like a little tweak to the above funcations would correct it.



[user@BS001 stage]$ ./gigahash.sh
____________________________________
11
eabcd
____________________________________


____________________________________
4422
bcda
____________________________________
11
dabc
____________________________________
321
abcd
____________________________________
[user@BS001 stage]$</description>
		<content:encoded><![CDATA[<p>The above didn&#8217;t work for the following example:</p>
<p>[user@BS001 stage]$ cat gigahash.sh<br />
#!/bin/bash</p>
<p>mhash=&#8221;abcd:55 bcda:4422 cdab:321 dabc:2131 eabcd:11&#8243;;</p>
<p>function hashv {<br />
        hkey=&#8221;";<br />
        mh=&#8221;";<br />
        if [[ $2 != "" ]]; then hkey=$2; else echo &#8220;&#8221;; return 0; fi<br />
        if [[ $1 != "" ]]; then mh=$1; else echo &#8220;&#8221;; return 0; fi</p>
<p>        echo $mh|sed -e &#8220;s/.*\([ \t]*\)\($hkey\):\([^ \t]*\?\)\([ \t]*\).*/\3/gi&#8221;<br />
}</p>
<p>function hashk {<br />
        hvalue=&#8221;";<br />
        mh=&#8221;";<br />
        if [[ $2 != "" ]]; then hvalue=$2; else echo &#8220;&#8221;; return 0; fi<br />
        if [[ $1 != "" ]]; then mh=$1; else echo &#8220;&#8221;; return 0; fi</p>
<p>        echo $mh|grep &#8220;$hvalue&#8221;|sed -e &#8220;s/\([^ \t]*\)[:]\($hvalue\)/\|\1|\2\|/i&#8221; -e &#8220;s/.*\?[|]\(.*\)[|].*[|].*\?/\1/i&#8221;;<br />
}</p>
<p>echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;eabcd&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;11&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;&#8221;;<br />
hashk &#8220;&#8221; &#8220;11&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;bcda&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;4422&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;abcd&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;2131&#8243;;<br />
echo &#8220;____________________________________&#8221;;<br />
hashv &#8220;$mhash&#8221; &#8220;cdab&#8221;;<br />
hashk &#8220;$mhash&#8221; &#8220;55&#8243;;<br />
echo &#8220;____________________________________&#8221;;</p>
<p>[user@BS001 stage]$</p>
<p>Now when I run the above script, &#8220;11&#8243; in<br />
&#8220;11<br />
dabc<br />
&#8221; is wrong. i.e. &#8220;hashv &#8220;$mhash&#8221; &#8220;abcd&#8221;;&#8221; failed to print the correct value of 55.. it printed 11 (which is for eabcd hash key&#8217;s value). Looks like a little tweak to the above funcations would correct it.</p>
<p>[user@BS001 stage]$ ./gigahash.sh<br />
____________________________________<br />
11<br />
eabcd<br />
____________________________________</p>
<p>____________________________________<br />
4422<br />
bcda<br />
____________________________________<br />
11<br />
dabc<br />
____________________________________<br />
321<br />
abcd<br />
____________________________________<br />
[user@BS001 stage]$</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BASH, HASH and AWK - The UNIX and Linux Forums</title>
		<link>http://www.microdevsys.com/WordPress/2009/02/06/creating-associative-or-hash-arrays-in-bash-using-sed-and-strings-without-the-use-of-arrays-looping-and-conditionals/#comment-6759</link>
		<dc:creator>BASH, HASH and AWK - The UNIX and Linux Forums</dc:creator>
		<pubDate>Sun, 07 Feb 2010 11:52:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.microdevsys.com/WordPress/?p=995#comment-6759</guid>
		<description>[...]  Might not be exactly what you're looking for but...  With regards to above, you could try this: Creating associative or hash arrays in bash using sed and strings without the use of arrays, looping... You'll have to tweak that code for your tastes though to use different delimeters depending on [...]</description>
		<content:encoded><![CDATA[<p>[...]  Might not be exactly what you&#8217;re looking for but&#8230;  With regards to above, you could try this: Creating associative or hash arrays in bash using sed and strings without the use of arrays, looping&#8230; You&#8217;ll have to tweak that code for your tastes though to use different delimeters depending on [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<span style="color: #999999; font-size: 100%">Technorati Key: JDKEMU44HHYG</span>

