LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2008, 11:33 AM   #1
jjhall
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Rep: Reputation: 0
AWK: problem getline inside while loop


Hello,

I am having a disconcerting problem regarding awk. I am trying to read certain lines from a data file. The line numbers which I am reading are stored in a separate file.

Code:
BEGIN {
  file1 = "extract_t0.0500.dat"
  i = 29
}

{
  for (j=1;j<i;j++) {
    file_in = "pointlist_x"bin[j]".txt"
    while ((getline < file_in) > 0) {
      command = "tail +"$1" " file1 " | head -1"
      print command
      command | getline 
      print $1 " " NF
      printf("%5.4f \t%5.4f\n",$5,$7)
    }
  }
}
Note that "pointlist_x*.txt" are files with a single column of integers that correspond to the line numbers that I want to read in file1. The output of this script gives, for example:

Code:
tail +1050 extract_t0.0500.dat | head -1
(The output of the printf statement is a blank line, or null string.) Now, if I run the command

Code:
tail +1050 extract_t0.0500.dat | head -1 | awk '{printf("%5.4f \t%5.4f\n",$5,$7)}'
in the shell, I get floating point values as I expect.

Can someone explain why getline is behaving oddly in this situation?

Thanks in advance for the help.
 
Old 02-27-2008, 12:43 PM   #2
jjhall
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
I may have figured out the problem. The for loop is in the body of the script, and would therefore be applied to each line of the input file. The getline command seems to be operating on the input file and ignoring the command that was supplied to it.

By putting an END statement before that chunk of code, I can fix the problem. I have noticed, however, that I must still supply an input file, e.g.

Code:
awk -f test.awk extract_t0.0500.dat
Is an input file required on the command line for invoking awk?
 
Old 02-27-2008, 03:19 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
or use associative arrays -
Code:
awk '
      FILENAME=="file2" { arr[sprintf("%d",FNR)]=$0; }
      FILENAME=="file1" { if(arr[sprintf("%d", $1)]) { 
      	        	    print arr[sprintf("%d", $1)]
      	                }
      }
    ' file2 file1
input and output:
Quote:
/home/jmcnama> cat file1
1
3
6
8
/home/jmcnama> cat file2
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
/home/jmcnama> t.awk
this is line 1
this is line 3
this is line 6
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use grep inside awk? Helene Programming 10 09-29-2015 08:48 PM
simple awk question getline coldy78 Programming 3 04-20-2007 11:39 PM
awk loop related kalyanofb Programming 3 02-16-2007 02:31 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 01:34 PM
awk inside a makefile linux.fob Programming 2 10-12-2005 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration