Computing.Net > Forums > Unix > awk getline issue

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk getline issue

Reply to Message Icon

Name: jvjaimes
Date: March 13, 2009 at 14:51:31 Pacific
OS: unix
Subcategory: General
Comment:

I am trying to look for a string in the first line and if this string is valid I will going down one line and get another validation, if this validation is also ok, i will going down again and print a parameter of if line, here is what a have:

140036.log 102.117.135.00
140036.log 02/27/2009 FAILURE
183422.log 102.117.135.00
183422.log 02/27/2009 SUCCESS
183422.log /CSPGM02_full_refresh//CED/SCED_20090227183439
183321.log 102.117.135.00
183321.log 02/27/2009 SUCCESS
183321.log /physical/switch/CGV/SCGV20090227183341.VM.DAT

and here is the awk that I want to use but is not working
awk '{
if ($2 = 102.117.135.00)
a=$2
getline
if ($3 = SUCCESS)
getline
print p" "$2}'



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: March 13, 2009 at 16:24:17 Pacific
Reply:

A single equal sign assigns a value.  To test for a value, you need a double equal sign.

String values need to be in double-quotes, otherwise they are assumed to be a variable.

Following an if-condition, awk assumes only one statement.

   if (a==1)
      statement1
      statement2

In the above, statement1 is optional based on value of a.  statement2 is not associated with the if-test, and thus will always be executed.  If you want both statements to be based on the if-test, then do:

   if (a==1)
      {statement1
       statement2}


In your original code, p" "$2 actually means the variable p followed by a space followed by word2. But p is unassigned, so I am guessing that you wanted a literal p, so I put it inside the quotes.

awk '{
if ($2 == "102.117.135.00")
   {a=$2
    getline
    if ($3 == "SUCCESS")
       {getline
        print "p "$2
       }
   }
}' file.in


And just for variety, here is a completely different way to code it:

awk '{
if ($2 != "102.117.135.00") next
a=$2
getline
if ($3 != "SUCCESS") next
getline
print "p "$2
}' file.in


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: awk getline issue

calcuate the week number of a given www.computing.net/answers/unix/calcuate-the-week-number-of-a-given/7128.html

Pause during gawk getline loop www.computing.net/answers/unix/pause-during-gawk-getline-loop/4556.html

weeknumber out of given date www.computing.net/answers/unix/weeknumber-out-of-given-date/6585.html