Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-27-2007, 11:56 AM
|
#1
|
|
Member
Registered: Mar 2003
Posts: 178
Rep:
|
How can I append text to a string in a file
I was wondering if there's a way I can add text to a specific string in a file without opening it. For example, if I wanted to add a group to geneanthony in /etc/group is there a way I can search the file for any string with geneanthony in it and then append to the end ,users? Is there a way to append on the next line after a string so I can keep a file organized? For example, I like to put certain text near other text is this possible?
|
|
|
|
02-27-2007, 12:11 PM
|
#2
|
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware, Cross Linux from Scratch, Gentoo
Posts: 2,663
Rep: 
|
If I understand your questions, sed should do what you want. Here's a quick primer http://tldp.org/LDP/abs/html/x19673.html
|
|
|
|
02-27-2007, 02:31 PM
|
#3
|
|
Member
Registered: Mar 2003
Posts: 178
Original Poster
Rep:
|
Reply
Thank you I'm looking it up now!
|
|
|
|
02-27-2007, 02:48 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
You can't do anything to a file without opening it!!
To search for a specific string and add to it:
sed "s/string/stringplusnewstuff/g" <oldfile >newfile
Here is a good tutorial on SED
|
|
|
|
03-01-2007, 12:16 PM
|
#5
|
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware, Cross Linux from Scratch, Gentoo
Posts: 2,663
Rep: 
|
Code:
sed -i 's:geneanthony:&,users:g' /etc/group
will append the string ",users" to every instance of the string "geneanthony" in file /etc/group. All that redirect stuff creates new files and is unnecessary.
Last edited by weibullguy; 03-01-2007 at 12:19 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:23 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|