Skip to content

Instantly share code, notes, and snippets.

@brianredbeard
Created April 11, 2018 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianredbeard/69090291f40348e5a6daaa83d4757787 to your computer and use it in GitHub Desktop.
Save brianredbeard/69090291f40348e5a6daaa83d4757787 to your computer and use it in GitHub Desktop.
An awk script for LDIF processing
# Set the following values before any processing occurs:
# RS (record separator) to an empty line
# FS (field separator) to a newline
# OFS (output field separator) to a comma
BEGIN { RS = "" ; FS = "\n" ; OFS=","}
{ for (i=1;i<=NF;i++) {
# Add a line for each field to be extracted, replacing the field name in
# all three spots:
if ($i ~ "^cn: ") { cn=gensub("^cn: ","","g",$i) }
if ($i ~ "^title: ") { title=gensub("^title: ","","g",$i) }
if ($i ~ "^uidNumber: ") { uidNum=gensub("^uidNumber: ","","g",$i) }
if ($i ~ "^uid: ") { uid=gensub("^uid: ","","g",$i) }
}
# Output content in the event that the fields are not blank
if ( cn > "" && title > "" && uid > "" && uidNum > "" ) {
printf("%s\n title: %s\n uid: %s\n login: loginprefix-%s\n\n", cn, title, uid, uid)
}
# make sure to reset the values to blank
cn="" ; title = ""; uid="" ; uidNum=""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment