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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: reading into an array and then print the value corresponding to index
# 15  
Old 06-24-2009
sorry about the curlies - here's a newER version with comments:
Code:
awk '
   # dealing with the FIRST specified file - opnoise 
   # for every line/record in the FIRST file, create an array (arr) indexed by FNR
   # with the value of the first field ($1)
   # go to the next record/line (next)
   FNR==NR {arr[FNR]=$1; next} 

   # Here we're dealing with the SECOND file specified on the command line - (opwflightno)
   # if the array (arr) cell indexed by FNR equal the value of the first field ($1) - 
   # print the entire record/line.
   $1==arr[FNR]
' opnoise opwflightno > opwono

# 16  
Old 06-24-2009
I am beginner, and would like to know the following

* Does the command read the files one by one, or does it read it simultaneously?

* How does it know which command is for which file?

* How does this command work without any curlies, if statement or print statement.

* In my case opnoise has 1000 unique records(2, 7, 12 and so on ) with just one field, where as opwflightno has 4560500 records with repetitive first field, and other fields of course. For example, it has 5000 records with first field value 2, 3000 records with first field value 3, 1200 records with first field 7 and so on.

*How do I get all the records from opwflightno that do not have first field equal to the first field of opnoise? In reference to the above example I would want all the records with first field equal to 3 and disregard the ones with first field 2 and 7.
# 17  
Old 06-24-2009
Quote:
Originally Posted by akshaykr2
I am beginner, and would like to know the following

* Does the command read the files one by one, or does it read it simultaneously?
one by one
Quote:
Originally Posted by akshaykr2
* How does it know which command is for which file?
As the comments outlined, the condition 'FNR==NR' is true only when reading the FIRST file
Quote:
Originally Posted by akshaykr2
* How does this command work without any curlies, if statement or print statement.
which command? ' '$1 == arr[FNR]'? If the condition is true and there's no
associated 'action', the default action is 'print $0'.
Quote:
Originally Posted by akshaykr2
* In my case opnoise has 1000 unique records(2, 7, 12 and so on ) with just one field, where as opwflightno has 4560500 records with repetitive first field, and other fields of course. For example, it has 5000 records with first field value 2, 3000 records with first field value 3, 1200 records with first field 7 and so on.
What is the question?
Quote:
Originally Posted by akshaykr2
*How do I get all the records from opwflightno that do not have first field equal to the first field of opnoise? In reference to the above example I would want all the records with first field equal to 3 and disregard the ones with first field 2 and 7.
Code:
awk 'FNR==NR {f1[$1];next} !($1 in f1)' opnoise opwflightno > opwono

# 18  
Old 06-24-2009
Thanks a lot! I really appreciate your help

---------- Post updated at 05:47 PM ---------- Previous update was at 05:23 PM ----------

What values are assigned to array f[$1]? I understand its indexed by $1 of file opnoise. Or I guess the index does not matter in this case, its just creates an array with all the records in $1 of file opnoise. Am I thinking right? Could you please further explain the meaning of f1[$1].
# 19  
Old 06-24-2009
Quote:
Originally Posted by akshaykr2
Thanks a lot! I really appreciate your help

---------- Post updated at 05:47 PM ---------- Previous update was at 05:23 PM ----------

What values are assigned to array f[$1]? I understand its indexed by $1 of file opnoise. Or I guess the index does not matter in this case, its just creates an array with all the records in $1 of file opnoise. Am I thinking right? Could you please further explain the meaning of f1[$1].
'f1[$1]' - creates a 'cell' in array 'f1' indexed' by '$1' with no associated value - "we ain't need no st*ing values" in this case.
'f1[$1]' - creates a 'cell' in array 'f1' indexed' by '$1' with no associated value - "we ain't need no st*ing values" in this case.
# 20  
Old 06-24-2009
Clear! Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

awk print in one line after reading textfile with paragraphs

Hello everybody I have a text file which has the following format: nmm "text20140601.033954text" "text" "text" "text" , ... , "text" "text" , ... , Lat 36.3247 Lon 16.0588 Depth 8 "text", ... , "text" "text", ..., CovXX 1.65 CovYY 2.32 CovZZ 1.2 "text" , ..., "text nmm ... (6 Replies)
Discussion started by: phaethon
6 Replies

3. UNIX for Dummies Questions & Answers

How To Print Array in awk?

Hello, May i please know how do i print the array using awk script. I am using below shell script to start with but not working. #!/bin/bash LOADSTATUS="Line 0" LOADSTATUS="Line 1" LOADSTATUS="Line 2" LOADSTATUS="Line 3" LOADSTATUS="Line 4" awk ' BEGIN { Your File Load Status }... (1 Reply)
Discussion started by: Ariean
1 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

how to search array and print index in ksh

Hi, I am using KSH shell to do some programming. I want to search array and print index value of the array. Example.. nodeval4workflow="DESCRIPTION ="" ISENABLED ="YES" ISVALID ="YES" NAME="TESTVALIDATION" set -A strwfVar $nodeval4workflow strwfVar=DESCRIPTION=""... (1 Reply)
Discussion started by: tmalik79
1 Replies

7. Shell Programming and Scripting

awk reading many fields to array

I want to read $3,$4,$5,$6,$7 of fileA in array and when fileb $1 = fileA $4 the i want to print array and few fields from fileB. This should work but has some syntax error. nawk -F, 'FNR==NR{a=;next} a{print a}' fileB fileA Appreciate if someone can correct this. (2 Replies)
Discussion started by: pinnacle
2 Replies

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

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