Sed to replace/merge line in etc/group

sed

I have a list of users from 1 command –

getent group ldap-group|cut -c 32-700

This gives me a list of users in the form

user1,user2,user3...

I would like to insert this into /etc/group under sshd

sshd:x:74:adminuser,root

And I'd like to keep all the text up until root.

I have tried this sed command but it does not actually modify the file correctly.

sudo sed -i "s/^(sshd:x:\\d+:root,).*/\\$1(getent group ldap-group|cut -c 32-700)/" /etc/group

Can anyone help me with the sed syntax?

Best Answer

I wouldn't try and do it as a one liner

groupappend=$(getent group ldap-group|cut -c 32-700)
sed -n "/^sshd:/s/$/,$groupappend/p" /etc/group

and if this works for you then

sed -i "/^sshd:/s/$/,$groupappend/" /etc/group

It wasn't clear that you would be running this multiple times, try

sed -i "/^sshd/c\sshd:x:74:adminuser,root,$groupappend" /etc/group

instead.

Related Topic