If first pattern is found, look for second pattern. If second pattern not found, delete line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If first pattern is found, look for second pattern. If second pattern not found, delete line
# 1  
Old 06-24-2013
If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... Smilie

I have the output of an ldapsearch that looks like this:

Code:
dn: cn=sam,ou=company,o=com
uidNumber: 7174
gidNumber: 49563
homeDirectory: /home/sam
loginshell: /bin/bash
uid: sam
fullName: Sam Goober
sn: Goober
objectclass: itUser
objectclass: inetOrgPerson
objectclass: itAuxAudit
objectclass: organizationalPerson
objectclass: Person
objectclass: ndsLoginProperties
objectclass: Top
objectclass: posixAccount
objectclass: shadowAccount
groupMembership: cn=UPWDL,ou=WDL,o=APPS
groupMembership: cn=CAD_UP,ou=CAD,o=APPS
groupMembership: cn=EQM_Manager,ou=EQM,o=APPS
groupMembership: cn=VPN_Default_Access,ou=VPN,o=APPS
groupMembership: cn=SQD_ProxyUsers,ou=SQD,o=APPS
groupMembership: cn=FMA_Admin,ou=FMA,o=APPS
groupMembership: cn=diwvgrp026,ou=IWV,o=APPS
cn: sam

What I'm trying to do is retain everything you see in the ldapsearch output except :

Code:
groupMembership: cn=UPWDL,ou=WDL,o=APPS
groupMembership: cn=CAD_UP,ou=CAD,o=APPS
groupMembership: cn=EQM_Manager,ou=EQM,o=APPS
groupMembership: cn=VPN_Default_Access,ou=VPN,o=APPS
groupMembership: cn=SQD_ProxyUsers,ou=SQD,o=APPS
groupMembership: cn=FMA_Admin,ou=FMA,o=APPS


In other words, I want to get rid of all lines that start with "groupMembership:cn=" unless they have a group name that starts with cn=diwvgrp* The groupMembership values returned by the ldapsearch on each user (there are about 200 users) may have one groupMembership that starts with cn=diwvgrp, or they may have several.

Code:
groupMembership: cn=diwvgrp026,ou=IWV,o=APPS
groupMembership: cn=diwvgrp027,ou=IWV,o=APPS
groupMembership: cn=diwvgrp052,ou=IWV,o=APPS
groupMembership: cn=diwvgrp086,ou=IWV,o=APPS

I always want to keep the groups that contain the line "diwvgrp" and always want to discard any other line that begins with "groupMembership but doesn't contain diwvgrpXXX (where XXX is a number between 000 and 999).

I also have to keep ALL the other lines that ldapsearch has returned. So using the above example, I need to turn it from what I posted above, into this:

Code:
dn: cn=sam,ou=company,o=com
uidNumber: 7174
gidNumber: 49563
homeDirectory: /home/sam
loginshell: /bin/bash
uid: sam
fullName: Sam Goober
sn: Goober
objectclass: itUser
objectclass: inetOrgPerson
objectclass: itAuxAudit
objectclass: organizationalPerson
objectclass: Person
objectclass: ndsLoginProperties
objectclass: Top
objectclass: posixAccount
objectclass: shadowAccount
groupMembership: cn=diwvgrp026,ou=IWV,o=APPS
cn: sam


I'd prefer to use bash scripting to do this, if possible. There is a list of names that the ldapsearch goes through, returning the above information for each of the 200 or so users. The command line I use to generate the file (slightly obfuscated) is:

Code:
ldapsearch -h superserver.company.com -p 389 -b "ou=company,o=organization" -L -f ./iwvnusers-test.txt "(cn=%s)" cn sn fullName Language passwordAllowChange uid uidNumber homeDirectory gidNumber loginshell objectclass groupMembership

Once I run that ldapsearch, I end up with a file with about 200 entries in it, each one of which looks similar to the example at the beginning of this post.

I've tried using grep -o and storing the output into a variable, deleting all the lines starting with "groupMembership" then taking what's in the variable and inserting it back into the text, but that's not working. It (along with a couple of other ways) I've found that will work with the output from a single user, but when run against a list of users, it doesn't replace the strings in the correct place.

If anyone has any thoughts on this, I would be most-grateful. If PERL is the only answer, I can deal with that, but would prefer to use Bash if it's not too unrealistic.

Thank you,
Sam
Moderator's Comments:
Mod Comment Please use CODE tags for input and output samples as well as for code segments.

Last edited by Don Cragun; 06-24-2013 at 06:24 PM.. Reason: Add CODE tags
# 2  
Old 06-24-2013
In a variety of tools from sed on up, your strategy must be to capture the lines starting with the header in a buffer until you find a next header or EOF, then if the first header is not to your liking, delete all but the second header line and return to get another line. In sed. this is /pattern/ match, b and : for loop, N to add another line to the buffer, $ to test EOF. The last group is irregular as it has no second header line. You might h the line, remove the second header if any, decide if it is a keeper, and the g the original buffer back. You might add a dummy final header to stdin using "( cat file ; echo dummy )|sed", but of course you need to remove it. You can d or "s/...//" what you dislike or "sed -n" and p what you like, but remember there is a second header in the buffer!
# 3  
Old 06-25-2013
pipe your ldapsearch to
Code:
awk '$1!~/^groupMembership:$/ || $2~/^cn=diwvgrp/'

Also possible with sed
Code:
sed '/^groupMembership:/{/cn=diwvgrp/!d;}'


Last edited by MadeInGermany; 06-25-2013 at 07:02 PM.. Reason: awk with OR
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies

2. Shell Programming and Scripting

prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say: ./rem Susan More than one match; Please select the one to remove: ... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

3. UNIX for Dummies Questions & Answers

Replace line with found unknown pattern

Hi, I have a file with the following content: --------- a 3242 tc_5 gdfg4 random text a 3242 tc_6 gdfg4 random text a 3242 tc_7 gdfg4 random text a 3242 tc_4 gdfg4 --------- I want to replace the lines containing tc_? (tc_5, tc_6 etc. even with unknown numbers) with the found... (5 Replies)
Discussion started by: joas
5 Replies

4. Shell Programming and Scripting

To find the line no, where the particular pattern is not found

Hi, suppose i have a txt file containing thye following data 2012156|sb3|nwknjps|BAYONNE|NJ|tcg 201221|094|mtnnjprc:HACKENSACK|NJ|tcg 201222|wn3|mtnnjtc|HACKENSACK|NJ|tcg 2018164|ik4|mtnntc|JERSEY CITY|NJ|tcg 20123482|ik4|mtnnjpritc,JERSEY CITY|NJ|tcg... (3 Replies)
Discussion started by: priyanka3006
3 Replies

5. Shell Programming and Scripting

Delete line if pattern not found

I thought that this was going to be quit simple using sed but i wasn't able to find a way to delete the second line of a text file if my pattern was not found in the line With awk i am completly useless :rolleyes: Any ideas? (2 Replies)
Discussion started by: jepeto
2 Replies

6. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

7. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies

8. Shell Programming and Scripting

change line found by pattern using sed

I want to change a line like CPM_THRESHOLD 0.8 // to a new value using sed I am trying sed -i "s/CPM_THRESHOLD/CPM_THRESHOLD\t$COH\t\t\/\//" $INPUT_4 but how can i substitute the whole line begining with CPM_THRESHOLD and substitute it? (2 Replies)
Discussion started by: larne
2 Replies

9. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

10. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies
Login or Register to Ask a Question