The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-21-2006
Registered User
 

Join Date: Feb 2006
Posts: 4
Awk Help

I am trying to transform an LDAP entry that looks like this:

/cn=Printer1/ou=HR/dc=N/A/dc=fabrikam/dc=com

I am using an awk script and am having a bit of trouble so any help is appreciated.

I am specifically trying to escape the "/" in dc=N/A so that it is quoted like dc="N/A". Actually, I would like to quote everything that is between the "=" and the next "/", so it looks like this.

/cn="Printer1"/ou="HR"/dc="N/A"/dc="fabrikam"/dc="com"

Becasue another issue I am having is related to spaces to the right of the "=".

For example:

/cn=Printer1 Building 1/ou=HR/dc=N/A/dc=fabrikam/dc=com

My algorithm - right now - is to split the parts into an array at the "=". If the array part contains more than one slash quote the whole thing then print the array part out.

Here is what I have so far:

#!/usr/bin/ksh

#DN=/cn=Printer1 Building 1/ou=HR/dc=N/A/dc=fabrikam/dc=com
DN=/cn=Printer1/ou=HR/dc=N/A/dc=fabrikam/dc=com

# Get the number of "=" in the DN and add 1 for the last segment
echo $DN|sed 's/\=/ EQUALS /g'|tr ' ' '\012'|sort|uniq -c|grep EQUALS>count

# this needs to be fixed to include # > 9 - works for now
VAL="$(cut -c4 count)"
VAL=$((VAL+1))

echo $DN | awk -v val=$VAL '{split($1, dnstring, "=");
for (i = 1; i <= val; i++)
print dnstring[i]}'
exit 0


As I have said, any help and/or advice is appreciated.

Thanks

Last edited by berrean; 08-11-2008 at 09:47 PM. Reason: Clean up for clarity - HTML tag showing up in question.
Reply With Quote
Forum Sponsor
  #2  
Old 02-21-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
Here's a start:
Code:
#!/usr/bin/ksh

for DN in "/cn=Printer1/ou=HR/dc=N/A/dc=fabrikam/dc=com" "/cn=Printer1 Building 1/ou=HR/dc=N/A/dc=fabrikam/dc=com"
do
    print "$DN"
    print "$DN" | nawk -F/ '{
        for (i=2; i<=NF; i++) {
            if (i <= (NF-1) && match($(i+1),"=")==0) {
                print $i"/"$(i+1)
                i++
            }
            else
                print $i
        }
    }'
    print
done
exit 0
This script works for both of your strings to build a list of values.
Reply With Quote
  #3  
Old 02-21-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Awk:
Code:
BEGIN { FS = OFS = "=" ; q = "\"" }
{ $NF = q $NF q
  for (i=2; i<NF; i++ )
  { match( $i, /.*\// )
    $i = q substr($i,1,RLENGTH-1) q substr($i,RLENGTH)
  }
  print
}
Reply With Quote
  #4  
Old 03-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 4
Thanks

Thank you very much tmarikle for your suggestions and help. I did have a minor problem with the quotes following the slash in the string per your suggestion, but resolved that. Once again - thank you to all who helped.

Here is my final function - for posterity's sake.

Code:
#--------------------------------
# function _quoteDN
#
# Purpose: Takes DN input and quotes the DN string values.
# 	   This function was designed specifically to handle a "/" within a value.
# 		
# Example input: /cn=Printer1 Building 1/ou=HR/dc=N/A/dc=fabrikam/dc=com
#        output: /cn="Printer1 Building 1"/ou="HR"/dc="N/A"/dc="fabrikam"/dc="com"
#
#-----------------------------------

function _quoteDN () {

      DN="$@"
      print "$DN" | awk -F/ '
      BEGIN {
         PLINE=""
         gsub(/"/,"") 	
      }
      {
        for (i=2; i<=NF; i++) {
            if (i <= (NF-1) && match($(i+1),"=")==0) {
			gsub(/=/, "=\"", $i)
			PLINE = PLINE "/" $i"/"$(i+1) "\"" 
		$(i+1)=""
		i++
   		}
            else 
		gsub(/=/, "=\"", $i)
       		if ($i != "") {
			PLINE = PLINE "/" $i "\""
		}
		else 
			PLINE = PLINE $i 
    	    	}
	}
	END { echo PLINE }'
return
}

Last edited by vgersh99; 03-01-2006 at 12:11 PM.
Reply With Quote
  #5  
Old 03-01-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
just a little suggestion if I may...
next time you post code, pls use the
PHP Code:
'[code]' and [/code
tags - it makes the code more readable - I've edited your post to ease the reading.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0