Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
A few notes. First, if you're definitely wanting to manually edit /etc/group, /etc/passwd, or /etc/sudoers you should use the secure edit commands for them, see:
Code:
man vipw
man vigr
man visudo
which adds failsafes to editing these critical files.
Second, as astrogeek suggested, again if editing these files is your actual goal then the administrative tools usermod, groupmod, and gpasswd may be of interest to you; again consult their man pages.
Finally, to answer the question posed in the title itself if the /etc/group was just a random example, you can use sed with a regular expression along the lines of:
Code:
sed -ie '3s/$/foo/' /path/to/file
which will append "foo" to the end of the third line of file. The form is '#s/pattern/replace/' and $ matches the end of a line which is why this works. For reference, ^ matches the beginning of a line.
A few notes. First, if you're definitely wanting to manually edit /etc/group, /etc/passwd, or /etc/sudoers you should use the secure edit commands for them, see:
Code:
man vipw
man vigr
man visudo
which adds failsafes to editing these critical files.
Second, as astrogeek suggested, again if editing these files is your actual goal then the administrative tools usermod, groupmod, and gpasswd may be of interest to you; again consult their man pages.
Finally, to answer the question posed in the title itself if the /etc/group was just a random example, you can use sed with a regular expression along the lines of:
Code:
sed -ie '3s/$/foo/' /path/to/file
which will append "foo" to the end of the third line of file. The form is '#s/pattern/replace/' and $ matches the end of a line which is why this works. For reference, ^ matches the beginning of a line.
what if instead of hardcoding line 3 in your example I wanted to use a variable like $line? I tried
what if instead of hardcoding line 3 in your example I wanted to use a variable like $line? I tried
Code:
sed -ie '$(echo $line) s/$/foo' /path
but no luck
First, make sure to have a / at the end of your search/replace sed expression and no space between the line number and 's', so foo/ rather than foo and )s rather than ) s; it's a requirement of that format.
Second you'll need to use double quotes to have bash expand variables; single quotes will have bash interpret things as literals. So:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.