awk array index help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk array index help
# 1  
Old 03-24-2009
awk array index help

Code:
$ cat file.txt
A|X|20
A|Y|20
A|X|30
A|Z|20
B|X|10
A|Y|40

Summing up $NF based on first 2 fields,

$ awk -F "|" 'BEGIN {OFS="|"}
{ sum[$1 OFS $2] += $NF }
END { for (f in sum) print f,sum[f] }
' file.txt

o/p:

A|X|50
A|Y|60
A|Z|20
B|X|10

Works !!

Wondering if we can assign the "$1 OFS $2" part in some variable so that

$ var="$1 OFS $2"

And something like this will work 

$ awk -v x=$var -F "|" 'BEGIN {OFS="|"}
{ sum[x] += $NF }
END { for (f in sum) print f,sum[f] }
' file.txt

Presently its not working. The reason I have posted this : I have to sum based on a lot of fields and I want to make it kind of configurable outside awk. Please.

# 2  
Old 03-24-2009
Code:
$ var="$1 OFS $2"

This won't do it, because in this context $1 and $2 are shell's positional parameters, ( possibly null values ), and OFS is just a string with no special value, something that you didn't mean to do.

You can try something like the following...

Code:
awk -v x=1 -v y=2  -F'|' '{ sum[ $x FS $y ] += $NF }  END { for (f in sum) print f, sum[f] }'  OFS='|' file

A|X|50
A|Y|60
A|Z|20
B|X|10

... and assign whatever integer values to x and y.
# 3  
Old 03-24-2009
Rubin,
My question was something like this.

I am trying to sum a few fields based on first 4 fields, something like this
Code:
.....
{  A1[$1 OFS $2 OFS $3 OFS $4 ] += $7;A2[$1 OFS $2 OFS $3 OFS $4 ] += $8;A3[$1 OFS $2 OFS $3 OFS $4 ] += $9 ..................

Since I have to repeat "$1 OFS $2 OFS $3 OFS $4" part everytime for all the fields I want to sum up, I just wanted to make it part of a variable, please help.
# 4  
Old 03-24-2009
Code:
awk 'BEGIN{OFS=FS=","}
{
key=sprintf("%s"OFS"%s"OFS"%s",$1,$2,$3)
a1[key]+=$4
a2[key]+=$5
}
END{
for(i in a1)
{
	print i" "a1[i]"  "a2[i]
}
}' filename

# 5  
Old 03-26-2009
Quote:
Originally Posted by summer_cherry
Code:
awk 'BEGIN{OFS=FS=","}
{
key=sprintf("%s"OFS"%s"OFS"%s",$1,$2,$3)
a1[key]+=$4
a2[key]+=$5
}
END{
for(i in a1)
{
    print i" "a1[i]"  "a2[i]
}
}' filename

Thank you very much summer_cherry.Its really helpful.

Could you please help me in making it a kind of configurable i.e.

Something like:

X will contain the fields based on which Y fields needs to be summed up; in the example you put above
X="1,2,3"
Y="4,5"
Thank you in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

2. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies

3. Shell Programming and Scripting

Index problem in associate array in awk

I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script. infile: S01 36407 53706 88540 S02 69343 87098 87316 S03 50133 59721 107923... (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

awk Search Array Element Return Index

Can you search AWK array elements and return each index value for that element. For example an array named car would have index make and element engine. I want to return all makes with engine size 1.6. Array woulld look like this: BMW 1.6 BMW 2.0 BMW 2.5 AUDI 1.8 AUDI 1.6 ... (11 Replies)
Discussion started by: u20sr
11 Replies

5. UNIX for Dummies Questions & Answers

awk: syntax for "if (array doesn't contain a particular index)"

Hi! Let's say I would like to convert "1", "2", "3" to "a", "b", "c" respectively. But if a record contains other number then return "X". input: 1 2 3 4 output: a b c X What is the syntax for: if(array doesn't contain a particular index){ then print the value "X" instead} (12 Replies)
Discussion started by: beca123456
12 Replies

6. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

7. Shell Programming and Scripting

awk: reading into an array and then print the value corresponding to index

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (19 Replies)
Discussion started by: akshaykr2
19 Replies

8. UNIX for Advanced & Expert Users

sql variable as array index

hi folks i am facing problom while trying to access sql variable as array index ina unix shell script....script goes as below.. #!/bin/ksh MAX=3 for elem in alpha beeta gaama do arr=$elem ((x=x+1)) Done SQL_SERVER='servername' /apps/sun5/utils/sqsh -S $SQL_SERVER -U user -P pwd -b -h... (1 Reply)
Discussion started by: sudheer157
1 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question

Featured Tech Videos