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
$

awk '{a[FNR]=$1} END{print a[20]}' opnoise
OR (better yet)
awk 'FNR==20 {print $1;exit}' opnoise

hmmm if you want to print the 20th element
this will do..

awk '{A[NR]]=$1}END{print A[20]}' opnoise 

and in you awk the print statement is outside the BEGIN block

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

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

nawk '
  FNR==NR {arr[FNR]=$1;next}
  $1 == arr[FNR]
' opnoise opwflightno > opwonoise

I am getting command not found. All I did was replace nawk with gawk. I do not have nawk.

ok then - run with either awk or gawk.

I did! its not working

My crystal ball is being repaired, so please elaborate.

$ gawk'{FNR==NR {arr[FNR]=$1; next} $1==arr[FNR]}' opnoise opwflightno > opwono
ise
bash: gawk{FNR==NR {arr[FNR]=$1; next} $1==arr[FNR]}: command not found

This is what I got

You're using a different command ...,
please re-read vgersh99's post.

Once again - try going through this thread and find what's been suggested.
Good luck!

I realized that you had not used curly brackets. The command runs, but the file generated(opwonoise) does not have anything in it.

Can you please explain the command?

Thanks

sorry about the curlies - here's a newER version with comments:

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

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.

one by one

As the comments outlined, the condition 'FNR==NR' is true only when reading the FIRST file

which command? ' '$1 == arr[FNR]'? If the condition is true and there's no
associated 'action', the default action is 'print $0'.

What is the question?

awk 'FNR==NR {f1[$1];next} !($1 in f1)' opnoise opwflightno > opwono

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.

Clear! Thanks a lot