Just need some guidance on this nawk | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Just need some guidance on this nawk

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-28-2013
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 22 April 2014, 7:16 AM EDT
Posts: 156
Thanks: 28
Thanked 0 Times in 0 Posts
Just need some guidance on this nawk

Hi,

I am trying to debug some KSH script and it has the nawk portion below. I just want to confirm whether I understand what it does correctly.

Example usage of the nawk line is run as below:


Code:
 
nawk '/^#/ {next}
$1~/^'testp.cfg'$|^'testp.cfg'\.testdb\.com\.ph$/ {
   c=0
   while (getline) {
     gsub (" ","")
     s=tolower($0)
     if (s~/host=/) {
        sub("^.*host=","",s)
        sub("\\\).*","",s)
        print s
        exit
     }
     c++
     if (c>20) {exit}
   }
}' $TNSNAMES

$TNSNAMES is a tnsnames.ora file, excerpts of some of its lines as below:


Code:
 
testd.testdb.com.ph = 
  (DESCRIPTION = 
        (ADDRESS = 
          (COMMUNITY = tcpip.world)
          (PROTOCOL = TCP)
          (Host = testd.testdb.com.ph)
          (Port = 25036)
        )
      (CONNECT_DATA = 
        (SID = testd)
           (GLOBAL_NAME = testd.testdb.com.ph)
      )
  )
testp.cfg = 
  (DESCRIPTION = 
        (ADDRESS = 
          (COMMUNITY = tcpip.world)
          (PROTOCOL = TCP)
          (Host = testp1)
          (Port = 5000)
        )
      (CONNECT_DATA = 
        (SID = testp)
           (GLOBAL_NAME = testp.cfg)
      )
  )

From what I understand, the nawk excludes all lines that begins with a # and if it finds a line that begins with testp.cfg or testp.cfg.testdb.com.ph, and starting from that line, it reads a line, strips the spaces, and convert it to lower case. Now I don't understand what this section of the nawk does?



Code:
     if (s~/host=/) {
        sub("^.*host=","",s)
        sub("\\\).*","",s)
        print s
        exit
     }
     c++
     if (c>20) {exit}


Does the first sub strips all spaces before the host= string? But what does the second sub do? Remove everything from the first dot (.)?

Is the c variable means it will only process 20 lines from when it find the testp.cfg or testp.cfg.testdb.com.ph string?

Any feedback much appreciated. Thanks in advance.
Sponsored Links
    #2  
Old 06-29-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 April 2014, 4:36 AM EDT
Location: Aachen, Germany
Posts: 3,534
Thanks: 51
Thanked 823 Times in 785 Posts
The spaces are removed by the gsub just before the tolower. Then, as you say, the host= and everything before is stripped off. I'm not sure what the second sub is supposed to do as it is looking for the \) string which does not exist in the line. And, the pattern could/should be anchored at line end using the $ sign.
And yes, it will stop 20 lines after the first search pattern is found.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need guidance on VMStat Snipper AIX 1 11-02-2012 08:20 AM
how to access values of awk/nawk variables outside the awk/nawk block? saniya Shell Programming and Scripting 5 05-13-2008 07:37 AM
A little guidance needed :) shadow0001 Shell Programming and Scripting 9 03-07-2008 01:16 PM
Career Guidance sufi_431 What is on Your Mind? 1 10-23-2007 10:54 AM
NIM Guidance scottsl AIX 1 06-12-2006 11:18 AM



All times are GMT -4. The time now is 08:12 AM.