Reading from one file and updating other file with constant entries

Hi All,

Thaks for the help in my last thread. I have one more question. I have two files with ldap entries in it. One file contains all the user LDAP parameter entries (26 MB) and other file contains only the user id's that have to be inactivated. Unix script has to read from the file that has user id information that has to be inactivated and update the main file with constant text. It has to update only those entries that are in the file that has the list of users to inactivate. Below is an example with input files and utput files that I need.

File 1

A:
uid: 1111
C:
D:
E:
 
A:
uid: 2222
C:
D:
E:
 
A:
uid: 3333
C:
D:
E:
 
A:
uid: 4444
C:
D:
E:

File 2

uid: 1111
uid: 3333
uid: 6666
.....

Output File

A:
uid: 1111
C:
D:
E:
nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com
nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com
nsaccountlock: true
 
A:
uid: 2222
C:
D:
E:
 
A:
uid: 3333
C:
D:
E:
nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com
nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com
nsaccountlock: true
 
A:
uid: 4444
C:
D:
E:

Thanks in advance
Samingla

Assuming there are no spaces between records in first file, like here:

A:
uid: 1111
C:
D:
E:
 <====== there should be no space here for this code to work!!
A:
uid: 2222
C:
D:
E:
awk -vRS= -vORS="\n\n" -vFS="\n" 'NR==FNR{for (i=1;i<=NF;i++){a[$i]}next}$2 in a{print $0"\nnsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com\nnsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com\nnsaccountlock: true";next}1' file2 file1

Thanks for the quick reply bartus11. we have that empty space after each user record. That new line divides different set of users in the file. Do we have any other option to process the file with the empty file.

Thanks,
Samingla

Newline is fine. It is even required. What I meant, was that there should not be a single space character. Let me color it up for you:
Bad:

A:
uid: 1111
C:
D:
E:
_
A:
uid: 2222
C:
D:
E:

Good:

A:
uid: 1111
C:
D:
E:

A:
uid: 2222
C:
D:
E:
nawk -f samig.awk file2 file1

samig.awk:

function p() {
    print "nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsaccountlock: true" ORS
}
FNR==NR{f2[$2];next}
/^uid:/ &&  $2 in f2 {f++}
!NF && f{
    p(); f=0; next
}
1
END {
  f && p()
}

try:

awk 'NR==FNR{a[$2]=1;next}a[$2]==1{p=5}NF==0{p=0}NF>0{p--}
p==1{$0=$0 RS "nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com\nnsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com\nnsaccountlock: true"}1' file2 file1

Hi,

Thanks for the update. I have a question regarding ignoring case sensitivity while searching from file 2. File 2 always has entries in CAPS. file 1 may contain the UID value in capitol or small letters. The current code is working only if it matches the case. Help in this is highly appriciated.

Thanks,
Sam

/^uid:/ &&  toupper($2) in f2 {f++}

1 Like

Hi Vgersh99,

It is working and thanks for the update.

Sam