Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Just need some guidance on this nawk

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-28-2013
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 11 November 2016, 12:28 AM EST
Posts: 215
Thanks: 40
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 Unix and Linux 06-29-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 7 December 2016, 1:03 PM EST
Location: Aachen, Germany
Posts: 9,924
Thanks: 200
Thanked 2,951 Times in 2,752 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
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:20 AM
how to access values of awk/nawk variables outside the awk/nawk block? saniya Shell Programming and Scripting 5 05-13-2008 08:37 AM
A little guidance needed :) shadow0001 Shell Programming and Scripting 9 03-07-2008 02:16 PM
Career Guidance sufi_431 What is on Your Mind? 1 10-23-2007 11:54 AM
NIM Guidance scottsl AIX 1 06-12-2006 12:18 PM



All times are GMT -4. The time now is 04:09 PM.