Skip to main content
added 34 characters in body
Source Link
Gilles Quénot
  • 36.8k
  • 7
  • 76
  • 97

I am trying to learn sed, but I'm having a lot of trouble. What I am trying to do is process my passwd file using a bash script with sed commands to do the following: For every user with group ID 20000, replace their GID in the file with 2000x, where x is the order of the first letter in the user's username (ie: a is 1, b is 2, etc.) Also, for every user whose default shell is bash, change their group to bash, and for those with shell tcsh, change their group to tcshgroup. I have done the above work in awk (much easier to work with I've found), but I don't even know where to start with sed. Any help is much appreciated.

Here is part of the passwd file: speech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false lightdm:x:110:118:Light Display Manager:/var/lib/lightdm:/bin/false avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false pulse:x:113:121:PulseAudio daemon,,,:/var/run/pulse:/bin/false saned:x:114:20000::/home/saned:/bin/tcsh mmccormick:x:1000:20000:owner,,,:/home/mmccormick:/bin/bash

speech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
lightdm:x:110:118:Light Display Manager:/var/lib/lightdm:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:113:121:PulseAudio daemon,,,:/var/run/pulse:/bin/false
saned:x:114:20000::/home/saned:/bin/tcsh
mmccormick:x:1000:20000:owner,,,:/home/mmccormick:/bin/bash

Ideally I'd pick out field 4 of each line to get the group ID and field 7 for the shell, but again, I'm not aware of a way to do that in sed. Thanks in advance.

I am trying to learn sed, but I'm having a lot of trouble. What I am trying to do is process my passwd file using a bash script with sed commands to do the following: For every user with group ID 20000, replace their GID in the file with 2000x, where x is the order of the first letter in the user's username (ie: a is 1, b is 2, etc.) Also, for every user whose default shell is bash, change their group to bash, and for those with shell tcsh, change their group to tcshgroup. I have done the above work in awk (much easier to work with I've found), but I don't even know where to start with sed. Any help is much appreciated.

Here is part of the passwd file: speech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false lightdm:x:110:118:Light Display Manager:/var/lib/lightdm:/bin/false avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false pulse:x:113:121:PulseAudio daemon,,,:/var/run/pulse:/bin/false saned:x:114:20000::/home/saned:/bin/tcsh mmccormick:x:1000:20000:owner,,,:/home/mmccormick:/bin/bash

Ideally I'd pick out field 4 of each line to get the group ID and field 7 for the shell, but again, I'm not aware of a way to do that in sed. Thanks in advance.

I am trying to learn sed, but I'm having a lot of trouble. What I am trying to do is process my passwd file using a bash script with sed commands to do the following: For every user with group ID 20000, replace their GID in the file with 2000x, where x is the order of the first letter in the user's username (ie: a is 1, b is 2, etc.) Also, for every user whose default shell is bash, change their group to bash, and for those with shell tcsh, change their group to tcshgroup. I have done the above work in awk (much easier to work with I've found), but I don't even know where to start with sed. Any help is much appreciated.

Here is part of the passwd file:

speech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false
lightdm:x:110:118:Light Display Manager:/var/lib/lightdm:/bin/false
avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:113:121:PulseAudio daemon,,,:/var/run/pulse:/bin/false
saned:x:114:20000::/home/saned:/bin/tcsh
mmccormick:x:1000:20000:owner,,,:/home/mmccormick:/bin/bash

Ideally I'd pick out field 4 of each line to get the group ID and field 7 for the shell, but again, I'm not aware of a way to do that in sed. Thanks in advance.

Source Link
Matt
  • 31
  • 1
  • 2

Using sed to process passwd file

I am trying to learn sed, but I'm having a lot of trouble. What I am trying to do is process my passwd file using a bash script with sed commands to do the following: For every user with group ID 20000, replace their GID in the file with 2000x, where x is the order of the first letter in the user's username (ie: a is 1, b is 2, etc.) Also, for every user whose default shell is bash, change their group to bash, and for those with shell tcsh, change their group to tcshgroup. I have done the above work in awk (much easier to work with I've found), but I don't even know where to start with sed. Any help is much appreciated.

Here is part of the passwd file: speech-dispatcher:x:108:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh colord:x:109:117:colord colour management daemon,,,:/var/lib/colord:/bin/false lightdm:x:110:118:Light Display Manager:/var/lib/lightdm:/bin/false avahi:x:111:120:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false hplip:x:112:7:HPLIP system user,,,:/var/run/hplip:/bin/false pulse:x:113:121:PulseAudio daemon,,,:/var/run/pulse:/bin/false saned:x:114:20000::/home/saned:/bin/tcsh mmccormick:x:1000:20000:owner,,,:/home/mmccormick:/bin/bash

Ideally I'd pick out field 4 of each line to get the group ID and field 7 for the shell, but again, I'm not aware of a way to do that in sed. Thanks in advance.