Jump to content

Upcctns script to replace a entry in ldif file

From TetraWiki
Revision as of 12:42, 27 March 2014 by Rakesh (talk | contribs) (Created page with " Scenario : There were two files("test", "test.ldif"), in 1st file("test") there were two fields, one field contained a numeric value(i.e. 772760042) and other field contained...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Scenario : There were two files("test", "test.ldif"), in 1st file("test") there were two fields, one field contained a numeric value(i.e. 772760042) and other field contained a string value(i.e. test str). In the 2nd file("test.ldif") there was a entry like "cn: 772760042".


"test" file:-

772760042, test str


"test.ldif" file:-

dn: uid=772760042,ou=upcctns.gov.in,dc=upcctns,dc=gov,dc=in

objectClass: top

objectClass: inetOrgPerson

objectClass: qmailUser

cn: 772760042 <---

sn: 772760042

mail: 772760042@upcctns.gov.in

accountStatus: active

mailMessageStore: upcctns.gov.in/772760042

uid: 772760042

mailHost: mail.upcctns.gov.in

deliveryMode: noforward


What was needed is we had to read the numeric value(i.e. 772760042) from 1st file("test") and search it in the 2nd file("test.ldif") and then replace the entry("cn: 772760042") with the 2nd field("test str") from 1st file("test")


After running the script the "test.ldif" file should look like

dn: uid=772760042,ou=upcctns.gov.in,dc=upcctns,dc=gov,dc=in

objectClass: top

objectClass: inetOrgPerson

objectClass: qmailUser

cn: test str <---

sn: 772760042

mail: 772760042@upcctns.gov.in

accountStatus: active

mailMessageStore: upcctns.gov.in/772760042

uid: 772760042

mailHost: mail.upcctns.gov.in

deliveryMode: noforward

Script:-

  #!/bin/bash
  INPUT_FILE=/home/tetra/Downloads/rakesh/test
  OUTPUT_FILE=/home/tetra/Downloads/rakesh/test.ldif
  #count=1
  while IFS=, read -r f1 f2 
  do
  #echo "cn: $f1" "cn: $f2"
  #echo $count pass
  #count=$(( $count + 1 ))
  awk -v old="cn: $f1" -v new="cn: $f2" '{ sub(old,new); print $0 }' "$OUTPUT_FILE" > ex
  cat ex > "$OUTPUT_FILE"
  done < $INPUT_FILE