I am trying to see if there is a way in awk to test the next record before processing.
All I am trying to do is see if the next line equal something then turn a flag off.
Example:
Problem
Cron
IneedThis
KeyOnThis
somemoredata
somemoredata
Cron
somemoredata
somemoredata
IneedThis
KeyOnThis
somemoredata
somemoredata
From the above I am trying to turn a flag off when I see IneedThis followed by KeyOnThis
Is this possible with awk?
I tried using:
if ($1 == "Cron" ) {
f = " "
}
But Cron appears in multiple places in my file. Is there a way to look ahead with getline?
Base on your sample data what is the required output.
No output. I just need to change a variable to null and process the next line output.
If we we know exactly what is to be done if true and what if not, then we can give better sugestion. SED also can do afew things, but not assign values. See ifthis is what u r looking 4.
old=''
while read new ; do
if [ "$new" = "KeyOnThis" -a "$old" = "IneedThis" ] ; then
myvar=""
fi
myvar="has a value"
old=$new
done < myfile.txt
return 0
danmero
October 15, 2009, 10:16pm
5
Something like that ?
awk '/IneedThis/{getline;flag=(/KeyOnThis/)?1:0}flag' file
Thanks for the reply's. I will try both when I get a chance.
Do you need to read ahead? Can't you do something like this?
nawk '
/IneedThis/{ i = NR }
/KeyOnThis/ && ( i == NR -1 ){ ++matched }
{ print }
(matched){
matched--
printf("%s\n", "f = \" \"")
}
' infile
OP: -
Problem
Cron
IneedThis
KeyOnThis
f = " "
somemoredata
somemoredata
Cron
somemoredata
somemoredata
IneedThis
KeyOnThis
f = " "
somemoredata
somemoredata
thanks much working like a charm!