Hi folks,
I've list of LDAP records in this format:
cat cmmac.export.tmp2
dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc
cmmac: 00:13:11:36:a5:06
dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc
cmmac: 00:15:96:1c:fa:23
dn: deviceId=0a92746a62sbms4662373470015961d6d7b,ou=devices,ou=customer,ou=nl,o=upc
cmmac: 00:15:96:1d:6d:7b
dn: deviceId=0a92746a64cbms466298898001596010372,ou=devices,ou=customer,ou=nl,o=upc
cmmac: 00:15:96:01:03:72
dn: deviceId=0a92746a64jbmr22a2083670015965883bc,ou=devices,ou=customer,ou=nl,o=upc
cmmac: 00:15:96:58:83:bc
Some of these records have cmmac attribute duplicity, so I don't want to process them further. I have list of duplicate records, so I'm going through source file and deleting cmmac: MATCH and dn: above. I've tried to utilize solution I've found on these forums
grep -v "$(grep -B 1 "MATCH" cmmac.export.txt)" cmmac.export.txt > cmmac.export.filtered
Everything would be fine, but bash is developing the command into
grep -v '"$(grep -B 1 "MATCH" cmmac.export.txt)"' cmmac.export.txt > cmmac.export.filtered which is not producing desired result.
Thank you for any ideas how to solve this.
Tomas
#--------------------------------------------------------
#Filtering duplicities from device exports
#--------------------------------------------------------
cp cmmac.export.tmp cmmac.export.tmp2
for fil in `grep cmmac cmmac.export.tmp | sort | uniq -c | grep -v " 1 " | awk -F' ' '{print $3}'`
do
echo $fil
grep -v \""\$(grep -B 1 \" $fil\" cmmac.export.tmp2)"\" cmmac.export.tmp2 > cmmac.export.txt
cp cmmac.export.txt cmmac.export.tmp2
done
+ cp cmmac.export.tmp cmmac.export.tmp2
++ grep cmmac cmmac.export.tmp
++ sort
++ uniq -c
++ awk '-F ' '{print $3}'
++ grep -v ' 1 '
+ for fil in '`grep cmmac cmmac.export.tmp | sort | uniq -c | grep -v " 1 " | awk -F'\'' '\'' '\''{print $3}'\''`'
+ echo 00:15:d0:00:a5:e6
00:15:d0:00:a5:e6
+ grep -v '"$(grep -B 1 " 00:15:d0:00:a5:e6" cmmac.export.tmp2)"' cmmac.export.tmp2
++ bashtrap
++ echo 'CTRL+C Detected '
---------- Post updated 13-01-12 at 05:52 AM ---------- Previous update was 12-01-12 at 01:10 PM ----------
Somehow I've found what to do:
Grep doesn't like putting the variable from "for" cycle, so I've wrapped it into echo call in subshell. I've no idea why it works this way.
for fil in $( grep cmmac cmmac.export.tmp | sort | uniq -c | grep -v " 1 " | awk -F' ' '{print $3}' );
do
#echo $fil
#Following line deletes line with pattern fil from file tmp2 and stores in txt
grep -v "$(grep -B 1 " `echo $fil`" cmmac.export.tmp2)" cmmac.export.tmp2 > cmmac.export.txt
cp cmmac.export.txt cmmac.export.tmp2
done