0

I'm removing entries in /etc/group programmatically.

Because I cannot use grep, cat, or cut for this exercise, I wrote my own program that can produce stdout and stdout data to essentially read a file. If you can write your solution in grep, awk, sed, cat, echo, etc. I can use it.

I have root access and can remove groups manually, but since n groups will contain a '+' character, I need a script that checks for this.

After first I assumed I could append any line including '+' with a #, but I'm now feeling confident that this isn't how you programmatically manage /etc/groups. I haven't found great documentation yet and was wondering if someone here might have a better idea on how to disable groups deemed 'legacy' via the use of '+' character.

6
  • 1
    try grep -v '+' /etc/group
    – cmak.fr
    Aug 20, 2019 at 14:35
  • I'm surprised by the suggestion. But perhaps I'm misreading the expression. Why should I be omitting results with '+'? Unless I redirect stdout to a new file and then leave all else omitted off of the new file? That's really clever, thanks, I think. Unless, I'm misunderstanding.
    – user981009
    Aug 20, 2019 at 14:48
  • Is this a homework problem?
    – user535733
    Aug 20, 2019 at 15:56
  • No, this is not a homework problem.
    – user981009
    Aug 20, 2019 at 16:31
  • 1
    yes the grep idea is fast and reliable. Take care to always have the /etc/group file exists. You can save the output of grep in a variable then rewrite the file :: newGroup=$(grep -v '+' /etc/group); echo "$newGroup" > /etc/group
    – cmak.fr
    Aug 20, 2019 at 20:28

1 Answer 1

1

yes the grep idea is fast and reliable. Take care to always have the /etc/group file exists. You can save the output of grep in a variable then rewrite the file :: newGroup=$(grep -v '+' /etc/group); echo "$newGroup" > /etc/group

You want to remove all lines containing the + sign in /etc/groups
Maybe you want to comment out the concerned lines

As quick as grep -v '+' /etc/group can do the job, here is :
- grep pattern file redirected to same file must use an intermediate file
- /etc/group can be backuped but must always exist

Better for same result : sed -i.backup '/pattern/d' file
- delete lines containing the pattern
- Create a backup file

Even better : Comment out matching lines & create backup
sed '/pattern/s/^/#/' file with -i<ext> option

# As root

# GREP
cp /etc/group /etc/group_backup      # Create a backup /etc/group_backup
newGroup=$(grep -v '+' /etc/group)   # Save new file content to var
echo "$newGroup" > /etc/group        # Rewrite /etc/group file from var

# SED
sed -i_backup '/+/d' file            # /pattern/d : delete lines containing the pattern
                                     # -i<ext>    : save edited stream to file and create backup file<ext>

# SED comment out & backup
sed -i_backup '/+/s/^/#/' /etc/group

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .