Help how to make awk array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help how to make awk array
# 1  
Old 01-02-2015
Help how to make awk array

Hi expert,
I'm newbie in awk programming and now I have difficulty how to make awk array in ash shell. I have code below.
As you know ash shell doesn't support array and I cannot install bash
in my embedded environment.
So in order this code to work in ash shell then all array needs to be converted
Problem now while creating array in ash shell :

Code:
   scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')

and calling array in ash shell :

Code:
   echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
   $HEYU $(echo ${scene[${phone#*~}]})

Can somebody help me on this?

Code:
#!/bin/sh

HEYU=/usr/bin/heyu	      # Path to Heyu
SED=/usr/bin/sed
SORTAZ=0                      # If 0, will sort by house code.

get_scenes() {
   SCENES=$($HEYU show usersyns | grep "usersyn")
   if [ "$SORTAZ" -eq "1" ]
    then 
       SCENES=$(echo "$SCENES" | sort)
   fi
   IFS=$(echo -en "\n\b")
   for line in $SCENES
    do
       let SCNCNT=$SCNCNT+1
       scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')
       echo -n "S: " >> $LOG
       echo $line | awk '{ print "SCENE~" $2 "~'$SCNCNT'" }' | sed 's/_/ /g' | \
       sed 's/.*/\L&/; s/[a-z]*/\u&/g' | tee -a $LOG
   done
   IFS=$ORIGIFS
}

set_scene() {
   echo -n "S: " >> $LOG
   echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
   $HEYU $(echo ${scene[${phone#*~}]})
   sleep 1
   get_updates "$DEVICES"
} 

get_scenes
set_scene


Last edited by Scrutinizer; 01-02-2015 at 04:06 PM.. Reason: additional code tags
# 2  
Old 01-02-2015
The awk array is very simple, just start using it and it is created. It is really a hash map. Your challenge is to learn enough awk to put the array to use. There are many fine online tutorials, just Google 'awk tutorial array', like: AWK Arrays Explained with 5 Practical Examples
# 3  
Old 01-02-2015
I suspect you will have more problems than just the arrays. For example the sed uses GNU extensions, which are likely not supported by the (busybox?) version in your embedded environment, nor is the -e option to echo. Also the code seems inefficient with several calls to subshells and external commands within a loop.

Could you provide a sample output of
Code:
/usr/bin/heyu | grep "usersyn"

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-02-2015
Quote:
Originally Posted by Scrutinizer
I suspect you will have more problems than just the arrays. For example the sed uses GNU extensions, which are likely not supported by the (busybox?) version in your embedded environment, nor is the -e option to echo. Also the code seems inefficient with several calls to subshells and external commands within a loop.

Could you provide a sample output of
Code:
/usr/bin/heyu | grep "usersyn"

yes, i need to install gnu sed instead of using busybox sed
gnu sed doesn't need to have lots of memory space, so i'm ok
but bash needs lots of memory, my embedded device can't have this

btw i have resolve the issue above by replacing

Code:
scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')

to
Code:
eval ${scene}${SCNCNT}=$(echo $line | awk '{ print $2 }')

Code:
echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG

to
Code:
echo "MSG~heyu $(echo $(eval echo \$$scene${phone#*~}))" | tee -a $LOG

Code:
$HEYU $(echo ${scene[${phone#*~}]})

to
Code:
$HEYU $(echo $(eval echo \$$scene${phone#*~}))

After replace, everything works as expected
However now I have another problem in my other code using array variable
The code is below
How to modify this code to work with ash shell ??

Code:
LIST=(1 2 3)
for i in "${LIST[@]}"; do
  echo "example.$i "
done


Last edited by Scrutinizer; 01-03-2015 at 01:21 AM.. Reason: CODE tags
# 5  
Old 01-03-2015
Try:
Code:
LIST="1 2 3"
for i in $LIST; do
  echo "example.$i "
done

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 01-03-2015
Quote:
Originally Posted by Scrutinizer
Try:
Code:
LIST="1 2 3"
for i in $LIST; do
  echo "example.$i "
done

thank's, it works
i have another array like this below
i can replace

Code:
$HEYU preset ${TH}${sparray[$TEMP]}

to
Code:
$HEYU preset ${TH}$(eval echo \$$sparray$TEMP)

however how to change this constant array?
Code:
sparray=([20]="1 17" [21]="1 18" [22]="1 19" [23]="1 20" [24]="1 21" \
               [25]="1 22" [26]="1 23" \
               [68]="3 1" [69]="3 2" [70]="3 3" [71]="3 4" [72]="3 5" \
               [73]="3 6" [74]="3 7" [75]="3 8" [76]="3 9" [77]="3 10" \
               [78]="3 11" [79]="3 12")

TEMP=$(echo ${phone} | awk -F'~' '{ print $4 }')
$HEYU preset ${TH}${sparray[$TEMP]}


Last edited by Scrutinizer; 01-03-2015 at 07:11 AM..
# 7  
Old 01-05-2015
Use linefeed as row sep and 'while read' to unpack:
Code:
LIST='1 a A
2 b B
3 c B'
 
echo "$LIST"|while read l
do
 echo "l=$l"
done
 
echo "$LIST"|while read x y z
do
 echo "x=$x y=$y z=$z"
done

If you have just one column with embedded whitespace, make it the last column so 'read ... z' scoops it up.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - make new line

Hi, I would like to make a new line example like below: "When you post an item you can just delete the files." every 3 words of the above sentence I would like to make a new line like below: " When you post an item you can just delete the files " and after the last line "the... (6 Replies)
Discussion started by: khchong
6 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

awk changes to make it faster

I have script like below, who is picking number from one file and and searching in another file, and printing output. Bu is is very slow to be run on huge file.can we modify it with awk #! /bin/ksh while read line1 do echo "$line1" a=`echo $line1` if then echo "$num" cat file1|nawk... (6 Replies)
Discussion started by: mirwasim
6 Replies

4. Shell Programming and Scripting

Reading columns from a text file and to make an array for each column

Hi, I am not so familiar with bash scripting and would appreciate your help here. I have a text file 'input.txt' like this: 2 3 4 5 6 7 8 9 10 I want to store each column in an array like this a ={2 5 8}, b={3 6 9}, c={4 7 10} so that i can access any element, e.g b=6 for the later use. (1 Reply)
Discussion started by: Asif Siddique
1 Replies

5. Shell Programming and Scripting

Make array from file

I need to create an array from the first line of a file like: a;b;c d;e;f g;h;i In this instance, the array should be (a,b,c). How do I do that? (1 Reply)
Discussion started by: locoroco
1 Replies

6. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

7. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

8. Shell Programming and Scripting

Is there anyway to make awk "talks" to DB, say any awk-ODBC tools

Hi all, Is there anyway to make awk talk to DB ? So my awk script reads the data from some text file and eventually those data need to be stored in database. So is there anyway to do it in awk all tohether, say any awk-ODBC tool to make it work? (I really hate to write a cpp/java program... (4 Replies)
Discussion started by: qiulang
4 Replies

9. Shell Programming and Scripting

awk help to make my work faster

hii everyone , i have a file in which i have line numbers.. file name is file1.txt aa bb cc "12" qw xx yy zz "23" we bb qw we "123249" jh here 12,23,123249. is the line number now according to this line numbers we have to print lines from other file named... (11 Replies)
Discussion started by: kumar_amit
11 Replies

10. Shell Programming and Scripting

to make 2 lines into 1 line using awk

suppose i have a file AAAHHHHAHH AJJJJAAAAAA AJJJJJAAJJAK AJJAJJAJKKK the output suhd be AAAHHHHAHHAJJJJAAAAAAAJJJJJAAJJAKAJJAJJAJKKK (2 Replies)
Discussion started by: cdfd123
2 Replies
Login or Register to Ask a Question