INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Build associative array

Build associative array

(OP)


Hi

I want to print a header and report with the fields selected. The issue is that my programm does not print the values.

my initial script

awk -v fields="1,2,3,4,5" '
BEGIN{
#header array
n[1]="ID"
...
n[103]="lastField"

l=split(fields,ARR,",")
for ( i=1;i<=l;i++){
printf i==l? n[ARR[i]] : n[ARR[i]]";" ## print header
var == "" ? var="$"ARR[i] : var=var"\";\"$"ARR[i] ## build associative array
}
print"\n"
print var
}{
val[var]++ ##Associative array
}END{
for ( i in val ) print i";"val[i] ## print associative array
} ' file.txt


output

ID;MSISDN;VERSION_ID;NODE_ID;MMSC_ENTITY
$1";"$2";"$3";"$4";"$5;1 # does not print the values

Any suggest
Thanks a lot
Malpa

RE: Build associative array

Try this:

Test data:

CODE

cat - <<! >emp.dat
198|Donald|OConnell|DOCONNEL|650.507.9833|6/21/2007|SH_CLERK|2600||124|50
199|Douglas|Grant|DGRANT|650.507.9844|1/13/2008|SH_CLERK|2600||124|50
200|Jennifer|Whalen|JWHALEN|515.123.4444|9/17/2003|AD_ASST|4400||101|10
201|Michael|Hartstein|MHARTSTE|515.123.5555|2/17/2004|MK_MAN|13000||100|20
202|Pat|Fay|PFAY|603.123.6666|8/17/2005|MK_REP|6000||201|20
203|Susan|Mavris|SMAVRIS|515.123.7777|6/7/2002|HR_REP|6500||101|40
204|Hermann|Baer|HBAER|515.123.8888|6/7/2002|PR_REP|10000||101|70
205|Shelley|Higgins|SHIGGINS|515.123.8080|6/7/2002|AC_MGR|12008||101|110
206|William|Gietz|WGIETZ|515.123.8181|6/7/2002|AC_ACCOUNT|8300||205|110
100|Steven|King|SKING|515.123.4567|6/17/2003|AD_PRES|24000|||90
! 
Script:

CODE

awk -F'|' -v fields="1,5,3,2,4" '
BEGIN{
#header array
h[1]="EMPLOYEE_ID";
h[2]="FIRST_NAME";
h[3]="LAST_NAME";
h[4]="EMAIL";
h[5]="PHONE_NUMBER";
h[6]="HIRE_DATE";
h[7]="JOB_ID";
h[8]="SALARY";
h[9]="COMMISSION_PCT";
h[10]="MANAGER_ID";
h[11]="DEPARTMENT_ID";
n=split(fields,arr,",");
print "fields="fields;
for(i in arr){printf "%s;",h[arr[i]]}; printf "\n"}
{for(i in arr){ k=arr[i]; printf "%s;",$k} printf "\n"}
' emp.dat 
Results:

CODE

==> ./m1
fields=1,5,3,2,4
PHONE_NUMBER;LAST_NAME;FIRST_NAME;EMAIL;EMPLOYEE_ID;
650.507.9833;OConnell;Donald;DOCONNEL;198;
650.507.9844;Grant;Douglas;DGRANT;199;
515.123.4444;Whalen;Jennifer;JWHALEN;200;
515.123.5555;Hartstein;Michael;MHARTSTE;201;
603.123.6666;Fay;Pat;PFAY;202;
515.123.7777;Mavris;Susan;SMAVRIS;203;
515.123.8888;Baer;Hermann;HBAER;204;
515.123.8080;Higgins;Shelley;SHIGGINS;205;
515.123.8181;Gietz;William;WGIETZ;206;
515.123.4567;King;Steven;SKING;100;
dora1:eintdev3:/opt/oracle/scripts/mom> 
3eyes

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: Build associative array

(OP)
Thanks a lot LKBrwnDBA

I made some changes

declare -a array
array=( "$@" )
len=${#array[@]}
file=${array[$len-1]}
args=${array[@]:0:$len-1}
echo "file: $file"
echo "Argumentos antes de $file son: $_args"
for ((i=0;i<$( expr ${#array[@]} - 1 );i++));do
echo "${array[$i]}"
if [ "$field" == "" ]; then
field=${array[$i]}
else
field=$field","${array[$i]}
fi
done
echo "-$field-"

awk -v fields=$field '
BEGIN{
#header array
n[1]="ID"
n[2]="MSISDN"
n[3]="VERSION_ID"
n[4]="NODE_ID";
...
n[103]="lastField"

l=split(fields,ARR,",")
for ( i=1;i<=l;i++){
printf i==l? n[ARR[i]] : n[ARR[i]]";" ## print header
}
print"\n"
}{
for ( i=1;i<=l;i++){ k=ARR[i]; if ( i==1) val=$k; else val=val" "$k }; Marc[val]++; val="" #Associative array
}END{
for ( i in val ) print i";"Marc[i] ## print associative array
} ' file.txt

Input file.txt

7|""|"4.5"|"mcomc11"|"mmh"|1023|1|"20130502015907Z"|"20130501205907"|0|1000|""||"B4F67C94-B202-71E2-8F51-78E7D15DF168"|"20130501015907Z"|"20130430205907"|86400|0|142768|||1||"image/jpeg;application/smil"|"142398;370"|""|""|"Personal"|"Normal"|""|4096||""|0|""|"573168229248"|""|""|""|""|""|""|"es"|0|""|1|"ou=Operator Community of Interest,ou=Operator ILN,o=acision.com"|0|""|"573122864305"|""|"732123"|""|""|""|""|"eng"|0|""|1|"ou=Operator Community of Interest,ou=Operator ILN,o=acision.com"|""|""||""|""|""|""|""|"AUsuJilYrzgJqMYFU"|""|""|""||""|""|""|""|""|""|"Neither"|"A"|0|||||""|""||||||""|""|""|""||"00"|""|""|""|""

Output File
./script.sh 6 file.txt

file: file.txt
Argumentos antes de file.txt son:
6
-6-
EVENT_TYPE

1001:86
1003:14
1004:49
1011:60
1012:24
1013:6
1021:49
1015:3
1023:84
1031:7
1040:13
1034:2
1053:2

Thanks a lot LKBrwnDBA

Question

Is it possible build "val" variable before to use awk. Then pass this variable "val" to awk to build the asociative array Marc[val]++ ??.

for example
fields selected
./script.sh 1 2 3 4 5 file.txt

#!/bin/bash

.......

val=$1" "$2" "$3" "$4" "$5

awk -v field=$val ' BEGIN{
...}
{
Marc[field]++
}END{ for ( i in Marc) print i":"Marc[i] }



Thanks
Malpa




RE: Build associative array



Can't see any "variable "val" or asociative array "Marc[val]++" in your script, but you could try something like this:

./script.sh 1,2,3,4,5 file.txt
#!/bin/bash
val=$1
... Etc ...

CODE

==> cat script.sh
#!/bin/bash
val=$1
fil=$2

awk -F'|' -v fields="$val" '
BEGIN{
#header array
h[1]="EMPLOYEE_ID";
h[2]="FIRST_NAME";
h[3]="LAST_NAME";
h[4]="EMAIL";
h[5]="PHONE_NUMBER";
h[6]="HIRE_DATE";
h[7]="JOB_ID";
h[8]="SALARY";
h[9]="COMMISSION_PCT";
h[10]="MANAGER_ID";
h[11]="DEPARTMENT_ID";
n=split(fields,arr,",");
print "fields="n":"fields;
for(i in arr){printf "%s;",h[arr[i]]}; printf "\n"}
{for(i in arr){ k=arr[i]; printf "%s;",$k} printf "\n"}
' $fil

==> ./script.sh 3,5,1 emp.dat
fields=3:3,5,1
PHONE_NUMBER;EMPLOYEE_ID;LAST_NAME;
650.507.9833;198;OConnell;
650.507.9844;199;Grant;
515.123.4444;200;Whalen;
515.123.5555;201;Hartstein;
603.123.6666;202;Fay;
515.123.7777;203;Mavris;
515.123.8888;204;Baer;
515.123.8080;205;Higgins;
515.123.8181;206;Gietz;
515.123.4567;100;King;
==> 
noevil


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: Build associative array

(OP)
LKBrwnDBA, Thanks a lot for your feedback

The script funtions well. But I would like to built a variable "val" (associative array), before to use awk. After to build this variable, pass it to awk.
The size of the val is variable, it depends of the amount of fields. this variable val contain the asociative array.

If i want to make a report, with 2 or more fields, the associative array will be val= $field1" "$field2..$fieldN.... I want to build this variable before to use awk. After I must to pass this variable to awk script--> awk -v val=$val ....
Into the main loop in awk, i am going to use this variable like a part of the associative array Marc[val]++. Then I print it

END{
for ( i in n ) print i":"Marc[i]
}


Piece of the programm that contain the logic.

....
{
gsub("\"","",$0);
$16=substr($16,1,10);
$8=substr($16,1,10);
for ( i=1;i<=l;i++){
k=ARR[i];
if ( i==1) val=$k; else val=val":"$k
};
Marc[val]++; val=""
}
....

./script.sh < fields > <file>



script.sh
---------

#!/bin/bash

declare -a array
array=( "$@" )
len=${#array[@]}
file=${array[$len-1]}
args=${array[@]:0:$len-1}
for ((i=0;i<$( expr ${#array[@]} - 1 );i++));do
#echo "${array[$i]}"
if [ "$field" == "" ]; then
field=${array[$i]}
else
field=$field","${array[$i]}
fi
done
#echo "-$field-"
#exit 0
awk -v fields=$field -v FS="|" '
BEGIN{
n[1]="ID";
n[2]="MSISDN";
n[3]="VERSION_ID";
n[4]="NODE_ID";
n[5]="MMSC_ENTITY";
n[6]="EVENT_TYPE"
...
n[103]="lastfield"

l=split(fields,ARR,","); for ( i=1;i<=l;i++){ printf i==l ? n[ARR[i]] : n[ARR[i]]":"; }; print":TOTAL\n"
}{
gsub("\"","",$0);
$16=substr($16,1,10);
$8=substr($16,1,10);
for ( i=1;i<=l;i++){
k=ARR[i];
if ( i==1)
val=$k;
else
val=val":"$k
};
Marc[val]++; val=""
}END{ for ( i in Marc ) print i":"Marc[i] } ' $file

Input file
----------

8|""|"4.5"|"mcomc11"|"mmh"|1011|1|"20130502235429Z"|"20130502185429"|0|1000|""||"A0FDCE18-B383-71E2-911E-78E7D15DF168"|"20130502235429Z"|"20130502185429"|86400|0|135666|||1||"application/smil;image/jpeg"|"284;135382"|""|""|"Personal"|"Normal"|""|4096||"1.0"|0|""|"573162722700"|""|""|""|""|""|""|"es"|0|""|1|"ou=Operator Community of Interest,ou=Operator ILN,o=acision.com"|0|""|"573102063606"|""|"732123"|""|""|""|""|"eng"|0|""|1|"ou=Operator Community of Interest,ou=Operator ILN,o=acision.com"|""|""||""|""|""|""|""|"AQ-gOrkfsvYXCYgZF"|""|""|"11_8423810_1367538869777"||"http://10.81.46.78:10021/mmsc/1_1?AQ-gOrkfsvYXCYgZ..."|""|"MMH11, http://192.168.222.181:51201/"|"unconfirmed"|"sms"|""|"Neither"|"A"|0|||||""|""||||||""|""|""|""||"00"|""|""|""|""
...
..

Output file
-----------

./script.sh 16 6 file.txt

SUBMIT_TIME_LOCAL:EVENT_TYPE:TOTAL

2013050218:1036:1
2013050218:1053:3
2013050218:1034:2
2013050218:1015:1
2013050118:1023:44
2013050218:1023:21
2013050217:1023:1
2013050218:1004:28
2013050218:1013:4
2013050211:1004:2
2013050218:1031:6
2013050118:1021:44
2013050218:1012:14
2013050218:1003:12
2013050217:1021:1
2013050218:1011:39
2013050218:1001:75

this is the output. -> Ok

Thanks a lot
Malpa

RE: Build associative array


Put it in "quotes":

CODE

./script.sh '3 5 7 1' file.txt
#!/bin/bash
val="$1"
fil=$2 
medal

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close