LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-08-2014, 07:19 PM   #1
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Rep: Reputation: 16
how to append text at the end of a line in a file


guyes lets say that I want to append a username to an existing group in the /etc/group file.. how could I do it?

existing line: users:x:1010
would like: users:x:1010:nancy

apending user nancy to end of existing line

thanks
 
Old 02-08-2014, 07:24 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I am not certain I understand the question, but from what I think you mean...

For the general case, just open the file in an editor and add the text to the desired line, then save the file.

In the specific case of /etc/group, you should use usermod to set the user's group memberships which will make sure the file is updated accordingly.

Last edited by astrogeek; 02-08-2014 at 07:27 PM.
 
Old 02-08-2014, 07:48 PM   #3
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
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.
 
1 members found this post helpful.
Old 02-09-2014, 03:22 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Assuming a unique line pattern, instead of a known numbered line
Code:
sed -r -i -e 's/(users:x:1010)/\1:nancy/' t.t
 cat t.t
users:x:1010:nancy
With shell var
Code:
t='(users:x:1010)'
sed -r -i -e "s/$t/\1:nancy/" t.t
 cat t.t
users:x:1010:nancy
note double quotes

Last edited by chrism01; 02-09-2014 at 03:32 AM.
 
Old 02-09-2014, 06:55 PM   #5
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Isaac Velando View Post
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
Code:
 sed -ie '$(echo $line) s/$/foo' /path
but no luck
 
Old 02-09-2014, 07:17 PM   #6
Isaac Velando
LQ Newbie
 
Registered: Feb 2014
Location: Texas
Distribution: Arch, Ubuntu Server, CentOS
Posts: 29

Rep: Reputation: 21
Quote:
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:

Code:
sed -ie "$(echo $line)s/$/foo/" /path
should do the trick.
 
1 members found this post helpful.
Old 02-09-2014, 10:05 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Code:
gpasswd -a nancy users
 
1 members found this post helpful.
Old 02-09-2014, 10:56 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
dugan is spot on

But for the sake of it

Code:
sed 's/users.*/&:nancy/' /path/to/file
& is the matched pattern
That _might_ be gnu sed specific, can't recall off the top of my head
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
How to append text to the second line of a file SentralOrigin Programming 23 11-06-2010 07:19 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration