Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by cmak.fr for Automating removal of legacy entries in /etc/group

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

Viewing all articles
Browse latest Browse all 2

Trending Articles