Copying corresponding entries based on search

Hi Guru's

I need some help to accomplish the below. I have two files, file 1 has ldap entries with one of its parameter cn. I have one more file with cn and corresponding row id. The out put should get the cn from file1 search for the cn in file2 and get the value of id and add the id entry in file 1. Below is the sample.

File 1:

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
 
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
 
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
 
# entry-id: 104
cn: test@test5.com
abc: 111
def: 222
xyz: 337
 

File 2

cn: test@test1.com, id: 1111
cn: test@test2.com, id: 2222
cn: test@test3.com, id: 3333
cn: test@test4.com, id: 4444
cn: test@test5.com, id: 5555
 

out put file:

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
id: 1111
 
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
id: 2222
 
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
id: 3333
 
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
id: 4444
 
# entry-id: 104
cn: test@test5.com
AccountLock: true
abc: 111
def: 222
xyz: 337
id: 5555
 

Thanks in advance for the help.
Samingla

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{print NF?$0:a[x]"\n";if(/^cn:/) x=$0}' FS="[, ]" file2 file1

hi Yinyuemi,

Thanks for reply. The one liner is working as expected. I need to add one more line in out put file using the same script. I have to add "object class: trs" after adding the id in the output file. Can you please update me with the script. The out put should be like the below.

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
id: 1111
object class: trs

# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
id: 2222
object class: trs


# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
id: 3333
object class: trs


# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
id: 4444
object class: trs


# entry-id: 104
cn: test@test5.com
AccountLock: true
abc: 111
def: 222
xyz: 337
id: 5555
object class: trs

Thanks in advance,
Samingla

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{print NF?$0:a[x]"\n""object class: trs\n";if(/^cn:/) x=$0}' FS="[, ]" file2 file1

Yinyuemi,

Thanks for the quick reply. The code is not working as expected. It is not adding the id value and I am seeing and extra space before the id. The out put is as below.

# entry-id: 104
cn: test@test5.com
AccountLock: true
abc: 111
def: 222
xyz: 337
 id: 
object class: trs
 

The output should be like this

# entry-id: 104
cn: test@test5.com
AccountLock: true
abc: 111
def: 222
xyz: 337
id: 5555
object class: trs

Thanks,
Samingla

Hi Samingla,
it seems to work for me:

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{print NF?$0:a[x]"\n""object class: trs\n";if(/^cn:/) x=$0}' FS="[, ]" file2 file1
# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
id: 1111
object class: trs
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
id: 2222
object class: trs
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
id: 3333
object class: trs
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
id: 4444
object class: trs
# entry-id: 104
cn: test@test5.com
abc: 111
def: 222
xyz: 337
id: 5555
object class: trs

Best,

Y

Hi Y,
Thanks for the update. I am seeing an issue here. In file1 I have some entries that are not related to file2. I mean, there are some entries in file1 that do not have an ralation to file2. With the current script, all the entries in file one are being added with the object class: trs. what I need is script has to check for entries from file2 if cn value found, update file1 with id and object class. if cn value not fuld in file2 no changes should be made for the entries. Below is the input and out put that I need. I really appriciate your help.

File 1:

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
 
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
 
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 102-2
cn: test@test02-2.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
 
# entry-id: 104
cn: test@test5.com
abc: 111
def: 222
xyz: 337
 
# entry-id: 104-2
cn: test@test04-4.com
abc: 111
def: 222
xyz: 337
 

File 2

cn: test@test1.com, id: 1111
cn: test@test2.com, id: 2222
cn: test@test3.com, id: 3333
cn: test@test4.com, id: 4444
cn: test@test5.com, id: 5555
 

out put file:

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
id: 1111
object class: trs
 
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
id: 2222
object class: trs
 
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
id: 3333
object class: trs
 
# entry-id: 102-2
cn: test@test02-2.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
id: 4444
object class: trs
 
# entry-id: 104
cn: test@test5.com
AccountLock: true
abc: 111
def: 222
xyz: 337
id: 5555
object class: trs
 
# entry-id: 104-2
cn: test@test04-4.com
abc: 111
def: 222
xyz: 337
 

Thanks,
Sam

Hi Sam,

try this:

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{if(a[x]) {print NF?$0: a[x]"\n""object class: trs\n"} else {print $0};if(/^cn:/) x=$0}' FS="[, ]" file2 file1

Best,

Y

Hi Y,
Output file is same as the file1. No changes are being made.

Thanks,
Sam

Hi Sam,

The below is output I generated, it looks fine.

Best,

Y

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{if(a[x]) {print NF?$0: a[x]"\n""object class: trs\n"} else {print $0};if(/^cn:/) x=$0}' FS="[, ]" file2 file1
# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
id: 1111
object class: trs
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
id: 2222
object class: trs
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
id: 3333
object class: trs
# entry-id: 102-2
cn: test@test02-2.com
abc: 111
def: 222
xyz: 335
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
id: 4444
object class: trs
# entry-id: 104
cn: test@test5.com
abc: 111
def: 222
xyz: 337
id: 5555
object class: trs
# entry-id: 104-2
cn: test@test04-4.com
abc: 111
def: 222
xyz: 337

Y,
I am nawk and below is the output that I am getting.

# entry-id: 100
cn: test@test1.com
abc: 111
def: 222
xyz: 333
 
# entry-id: 101
cn: test@test2.com
abc: 111
def: 222
xyz: 334
 
# entry-id: 102
cn: test@test3.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 102-2
cn: test@test02-2.com
abc: 111
def: 222
xyz: 335
 
# entry-id: 103
cn: test@test4.com
abc: 111
def: 222
xyz: 336
 
# entry-id: 104
cn: test@test5.com
abc: 111
def: 222
xyz: 337
 
# entry-id: 104-2
cn: test@test04-4.com
abc: 111
def: 222
xyz: 337

Thanks,
Sam

I'm using gawk, that might be the reason,
I'm not sure where is the problem, but you can try the below code, see if it works. if not, maybe other people using nawk, can help you. Sorry for this.

Best,

Y

 awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{if(length(a[x])>0) {print NF?$0: a[x]"\n""object class: trs\n"} else {print $0};if(/^cn:/) x=$0}' FS="[, ]" file2 file1

I am getting the same output again. I cannot use gawk. Appriciate all your help.

Thanks,
Sam

Hi Y,

How can I use toupper in the same script. I have a problem with the current script. The CN entry in file2 is always in capitol. The same CN entry in file1 may be in capitol lettors or small lettors. current script is working only if the CN value in file1 is in CAPS. How can we do this?

Thanks in advance,
Srikanth

Hi Sam,

you can try:

awk ' BEGIN{IGNORECASE=1}other codes' 

hope it works,

Best,

Y

Where should I use this?

like this:

awk 'BEGIN{IGNORECASE=1}NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{if(length(a[x])>0) {print NF?$0: a[x]"\n""object class: trs\n"} else {print $0};if(/^cn:/) x=$0}' FS="[, ]" file2 file1

Hi Y,

I have tested it and it is not working as expected.

thanks,
Sam

As noted in the other thread of yours..

awk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{if(length(a[x])>0) {print NF?$0: a[x]"\n""object class: trs\n"} else {print $0};if(/^cn:/) x=toupper($0)}' FS="[, ]" file2 file1

Hi vgersh99,

I tried it as from the other post. It did not work. Below is the code I am using.

nawk 'NR==FNR{a[$1" "$2]=$4" "$5}NR>FNR{print NF?$0:a[x]"\n";if(/^cn:/) x=toupper($0)}' FS="[, ]" f2 f1 > out

Is there any other way to accomplish this?

Thanks,
Sam