Index problem in associate array in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Index problem in associate array in awk
# 1  
Old 03-24-2015
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.
Code:
infile:
S01    36407    53706    88540
S02    69343    87098    87316
S03    50133    59721    107923
S05    1290    41074    135810
S06    11285    30164    40361
S07    11285    30164    40361
S10    11298    30165    40361
S11    18311    37266    135798
S12    14567    35958    62691

where S04, S08 & S09 are missing. And I am expecting the output as
Code:
S01    36407    53706    88540
S02    69343    87098    87316
S03    50133    59721    107923
S04    0    0    0
S05    1290    41074    135810
S06    11285    30164    40361
S07    11285    30164    40361
S08    0    0    0
S09    0    0    0
S10    11298    30165    40361
S11    18311    37266    135798
S12    14567    35958    62691

I have adapted the scripts of Schrutinizer from my previous post as
Code:
awk -v n=12 '
  BEGIN {
    OFS="\t"
    zero=OFS 0 OFS 0 OFS 0
  }
  {
    i=$1                 # Line 7
    A[i]=$0              # Line 8
  }
END{
      for(i = 1; i <=n ; i++) {
      id = sprintf("S%02d", i)
      print id (id in A?A[id]:zero)
  }
}
' infile

But the output is
Code:
S01S01    36407    53706    88540
S02S02    69343    87098    87316
S03S03    50133    59721    107923
S04    0    0    0
S05S05    1290    41074    135810
S06S06    11285    30164    40361
S07S07    11285    30164    40361
S08    0    0    0
S09    0    0    0
S10S10    11298    30165    40361
S11S11    18311    37266    135798
S12S12    14567    35958    62691

which has concatenated IDs in the first column.
The fix is to add an extra line between Line7 and Line8. $1="" which is to empty $1 after each line.
I need elaboration how this new line works in awk.
Thanks a lot!

Last edited by yifangt; 03-24-2015 at 05:29 PM..
# 2  
Old 03-24-2015
Your printing the [ICODE]id[/ICODE] twice.
You forgot to empty the first as field Scrtuinizer suggested.
hth

EDIT: Nevermind, fingers were too fast.
Sorry
This User Gave Thanks to sea For This Post:
# 3  
Old 03-24-2015
Thanks sea!
Yes, I did not realize that. A new fix is
Code:
print (id in A?A[id]:id" "zero)

But, my question is why after I added $1="" id is not printed twice?
# 4  
Old 03-24-2015
Quote:
Originally Posted by yifangt
Thanks sea!
Yes, I did not realize that. A new fix is
Code:
print (id in A?A[id]:id" "zero)

But, my question is why after I added $1="" id is not printed twice?
'cuz $0 in A[i]=$0 no longer contains the old $1
# 5  
Old 03-24-2015
And if I use another version
Code:
awk -v n=12 '
  BEGIN {
    OFS="\t"
    zero=OFS 0 OFS 0 OFS 0
  }
  {
     A[$1]=$0
  }
END{
      for(i = 1; i <=n ; i++) {
      id = sprintf("S%02d", i)
      print (id in A?A[id]:id" "zero)
  }
}
' file1

There is no need to empty $1 at all. But I still do not get answer for the part $1="".

---------- Post updated at 04:51 PM ---------- Previous update was at 04:47 PM ----------

Sorry, I still did not get that
Quote:
cuz $0 in A[i]=$0 no longer contains the old $1
Each line of the infile is uniq, I thought $1="" is not needed as awk automatic read one line after another. What did I miss?

Last edited by yifangt; 03-24-2015 at 06:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

awk array index help

$ 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 += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

6. Shell Programming and Scripting

Index problem with awk

Hello, I have this code: #!/bin/sh awk -v val="........." 'BEGIN{FS=OFS=" ";c=0} NR==FNR&&d==0{a=$0; c++;next} FNR==(NR-c){b=val;next} {if(1234 in a){print "okay"}} {print $1}' listi fpr.11 grn that is working (awk find the value in the table "a" and return "okay" followed by 1234) ... (2 Replies)
Discussion started by: jolecanard
2 Replies

7. Shell Programming and Scripting

Problem when assign the array with the string index

I come across the problems when assigning the array in the script below . How to use the array with the 'string index' correctly ? When I assign a new string index , the array elements that are previously assigned are all changed .:eek::eek::eek: $ array=211 $ echo ${array} 211 $... (4 Replies)
Discussion started by: youareapkman
4 Replies

8. 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

9. 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

10. Shell Programming and Scripting

associate array problems in awk

hi, i have 3 fields in a file and linked them through 2 associative arrays.. the problem is one of the associative array is working while the other is not.. the code part is: awk ' BEGIN { FS="|" rc = getline < "ICX_RULES" while ( rc == 1 ) { rule_id=$1 rule_parameter=$2... (2 Replies)
Discussion started by: aami_sayan
2 Replies
Login or Register to Ask a Question