Help in sed required.

Hi All,
I am facing a small problem in sed. I want to insert a line in the existing file.

Existing code:

access to attr=userPassword
 by self write
 by * auth
 
access to *
 by self write
 by users read
 by anonymous auth

Desired code:

access to attr=userPassword
 by self write
 by * auth
 
access to *
        by self write
        by users read
        by peername.ip=127.0.0.1 read
        by anonymous auth


I put the existing code in var1 and desired code in var2 and used the following sed command, but somehow it's not working for me. Can there be a easier way to do this.

sed -i -e 's/$var1/$var2/g;' slapd_config

Try this:

sed -i -e '/by users read/a\
        by peername.ip=127.0.0.1 read' slapd_config

Thanks Annihilannic . this works for me. But I have two such instanes of the same text in the same file and I need to make changes only in the 2nd instance.

Is this possible by tweaking this a little bit.

Thanks!
nua7

You could add something to skip up through "access to *" before making the substitution.

sed -e '1,/^access to \*/b' -e '/by users read/a\''by peername.ip=127.0.0.1 read' slapd_config

Thanks era! But this is not working. I guess I need to find out this text first

access to attr=userPassword
 by self write
 by * auth

After that find out the statement "by users read" and insert this statement after it .

by peername.ip=127.0.0.1 read

So the new code would be ,

access to *
 by self write
 by users read
 by anonymous auth
....
....
some lines here...
 
 
access to attr=userPassword
 by self write
 by * auth
 
access to *
        by self write
        by users read
        by peername.ip=127.0.0.1 read
        by anonymous auth

Hope I have not confused.

Hi,

check the following sed command

sed -n 's/${a}/${b}/g' file1

I am sorry , but can you please explain this to me. I ran the command but it did not make any change.

I substituted a with var1 and b with var2 , which I had set, but that didn't work too.

I'm not sure I understand. Do you mean skip down to "access to attr=userPassword" and then add the line like before, after any occurrence of "by users read"?

sed -e '1,/^access to attr=userPassword/b' \
    -e '/by users read/a\''by peername.ip=127.0.0.1 read' slapd_config

I assumed "access to" is at beginning of line; taking out the ^ if that is not so should not hurt at all, though.

I am really sorry for all the confusion. Following is the part of file and I want to add a line "by peername.ip=127.0.0.1 read
" where I have made the text bold.But I want to avoid the text mareked in Red.

# access to *
#       by self write
#       by users read
#       by anonymous auth
#
# if no access controls are present, the default policy is:
#       Allow read by all
#
# rootdn can always write!
######################################################################
#leave at default of 500
#sizelimit 200 
# acl ################################################################
#defaultaccess search
access to attr=userPassword
 by self write
 by * auth
access to *
 by self write
 by users read
 by anonymous auth

Just change the search string to include matching the beginning of the line:

sed -i -e '/^ by users read/a\
        by peername.ip=127.0.0.1 read' slapd_config

Hey!
Thanks a lot! This works perfectly fine except that it puts some spaces in the file.Any idea how to overcome this.

access to *
 by self write
 by users read
        by peername.ip=127.0.0.1 read
 by anonymous auth

Hi,

Check this

sed -i -e '/^ by users read/a\ by peername.ip=127.0.0.1 read' slapd_config

As such, the solution I posted appears to also work. It just passes through anything up through the l"access to attr=userPassword" line and then performs the addition of "by peername etc" after any match on "by users read".

Thanks thangaraju, era and Annihilannic! Your soln works like a charm!

perl -ne 'print $_;if(m/by users read/){$a++;}if($a==2){print "by peername.ip=127.0.0.1 read\n";$a++;}' filename