LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-27-2007, 11:56 AM   #1
HGeneAnthony
Member
 
Registered: Mar 2003
Posts: 178

Rep: Reputation: 30
Post 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?
 
Old 02-27-2007, 12:11 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
If I understand your questions, sed should do what you want. Here's a quick primer http://tldp.org/LDP/abs/html/x19673.html
 
Old 02-27-2007, 02:31 PM   #3
HGeneAnthony
Member
 
Registered: Mar 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Reply

Thank you I'm looking it up now!
 
Old 02-27-2007, 02:48 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 03-01-2007, 12:16 PM   #5
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pipe output to append to a text file davee Linux - Newbie 5 03-22-2016 07:44 PM
quickly append text to file blackzone Linux - General 3 09-07-2006 05:48 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 04:17 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:29 AM.

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