LinuxQuestions.org
Help answer threads with 0 replies.
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, 12:56 PM   #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, 01:11 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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, 03: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, 03:48 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 01:16 PM   #5
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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 01:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:44 PM
quickly append text to file blackzone Linux - General 3 09-07-2006 06:48 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 05:17 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 08:13 PM


All times are GMT -5. The time now is 03:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration