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...
I have the output of an ldapsearch that looks like this:
What I'm trying to do is retain everything you see in the ldapsearch output except :
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.
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:
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:
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:
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
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!
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)