gawk getline eats next line for processing
Good afternoon everyone,
I'm new to awk/gawk but I'm working on a simple data processing script. My script uses a loop with getline to check for the value on the next line to decide if it's time to terminate the loop. This works dandy, but the problem is that getline eats that line, which then isn't processed by the rules in the remainder of the script (even though I want it to be). To illustrate what I mean, consider this simple gawk script: Code:
{ Code:
{ My question is basically how to I reset the stream such that, after I do a getline, the line read by getline continues to be the next line executed by the script? Thanks for your help! |
Are you trying to manipulate the line you grab with getline outside of the gawk script? Can we see the larger context of what you're trying to do? It seems at first glance like you should be using gawks redirection but I'm simply guessing at this point.
Regards, Alunduil |
restructuring to the rescue
Imagine a file with a bunch of lines like this:
FUN bla N FUN bla2 bla3 N FUN bla4 N I want to turn it into this: FUN bla N FUN bla2;bla3 N FUN bla4 N So I was reading the next value of the line to see if it was a "FUN" record in order to break the loop. The problem was that, the "FUN" next line would get totally ignored. In other words, the line that started with "FUN", used to break the loop, didn't get processed by the script proper and so the "FUN bla4 N" line wouldn't get printed Code:
#!/usr/bin/gawk -f Anyway, I suppose it's still an interesting question if you understand the context. Basically "getline" eats the next line of input so that it can't be processed by the script, even if you want it to be, so I was wondering how, even if you use getline, you can make the script process the next line as if "getline" hadn't been called. |
It appears (from reading the gawk manual) that you cannot preform normal processing of the line stolen with getline. From the gawk manual:
Quote:
|
Interesting, on a side note perhaps sed would be easier to do the parsing you want to do?
Regards, Alunduil |
Getline is inappropriate as all calls to it change NR and FNR, sometimes other items as well, so the following iteration will always read the following line.
Your options are either to save the current line each time and then do your comparison on the next time through or you could try something like: Code:
awk 'ORS=/ N$/?"\n":(/[0-9]$/)?";":" "' file |
Quote:
It is actually possible to get the input of the file being read, as the following example shows: Code:
FNR == 1 { |
Well in a way you are still proving my point, although I do see where you are coming from. Whilst in your example getline is not altering NR and FNR of original file it is in fact altering
these values for the file being used by getline |
Quote:
Also, while "getline <filename" does advance the file pointer for the file it is reading, you can close() the file and start reading from the beginning again. |
All times are GMT -5. The time now is 02:21 PM. |