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
# 1  
Old 06-24-2009
Question 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[i]=$1}{print arr[20]}'

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 the problem.

The problem is when I execute the script there is no activity(no error, just a blank) and the body i.e, the print statement, execute only when I hit "ENTER" for the second time. Also gawk does not exit to command prompt automatically, I have to hit CTRL+C to exit and awk gets displayed while exiting.

Here is what happens,

user@server ~/folder
$ awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr[i]=$1}{print arr[20]}'

684
awk

user@server ~/folder
$
# 2  
Old 06-24-2009
Code:
awk '{a[FNR]=$1} END{print a[20]}' opnoise
OR (better yet)
awk 'FNR==20 {print $1;exit}' opnoise


Last edited by vgersh99; 06-24-2009 at 01:45 PM.. Reason: ooops - you needed $1
# 3  
Old 06-24-2009
hmmm if you want to print the 20th element
this will do..
Code:
awk '{A[NR]]=$1}END{print A[20]}' opnoise

and in you awk the print statement is outside the BEGIN block
# 4  
Old 06-24-2009
Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums
# 5  
Old 06-24-2009
Question

Actually this what I am trying to do. I need to use the values in the array to remove certain records in file opwflightno and make a new file opwonoise. But awk is not executing beyond BEGIN. I had posted a simple example to describe the problem.



awk 'BEGIN {for(i=1;(getline < “opnoise”)>0; i++) arr[i]=$1}{for(j=1; j<=i; j++){if(arr[j]!=$1){print $0}}}' opwflightno > opwonoise
# 6  
Old 06-24-2009
Code:
nawk '
  FNR==NR {arr[FNR]=$1;next}
  $1 == arr[FNR]
' opnoise opwflightno > opwonoise

# 7  
Old 06-24-2009
I am getting command not found. All I did was replace nawk with gawk. I do not have nawk.
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