Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build associative array 1

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
CO


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] : n[ARR]";" ## print header
var == "" ? var="$"ARR : var=var"\";\"$"ARR ## build associative array
}
print"\n"
print var
}{
val[var]++ ##Associative array
}END{
for ( i in val ) print i";"val ## 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
 
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
 
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] : n[ARR]";" ## print header
}
print"\n"
}{
for ( i=1;i<=l;i++){ k=ARR; if ( i==1) val=$k; else val=val" "$k }; Marc[val]++; val="" #Associative array
}END{
for ( i in val ) print i";"Marc ## 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 }



Thanks
Malpa




 


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
 
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
}


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;
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] : n[ARR]":"; }; print":TOTAL\n"
}{
gsub("\"","",$0);
$16=substr($16,1,10);
$8=substr($16,1,10);
for ( i=1;i<=l;i++){
k=ARR;
if ( i==1)
val=$k;
else
val=val":"$k
};
Marc[val]++; val=""
}END{ for ( i in Marc ) print i":"Marc } ' $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"||" ...
..

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
 

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top