Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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?

share|improve this question
up vote 1 down vote accepted

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


share|improve this answer
OK that sort of works but I would like to run the command every hour via cron and that will append multiple sshd lines to /etc/group? – somabc Jan 21 '14 at 21:24
@BryanCochrane: Nope. – Iain Jan 21 '14 at 21:26
When I ran the last command I ended up with a new identical sshd line added each time. – somabc Jan 21 '14 at 21:28
@BryanCochrane: I only get one sshd:... line on my CentOS system. – Iain Jan 21 '14 at 21:29
Thanks that is working! – somabc Jan 21 '14 at 21:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.