LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I append text to a string in a file (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-append-text-to-a-string-in-a-file-532970/)

HGeneAnthony 02-27-2007 11:56 AM

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?

weibullguy 02-27-2007 12:11 PM

If I understand your questions, sed should do what you want. Here's a quick primer http://tldp.org/LDP/abs/html/x19673.html

HGeneAnthony 02-27-2007 02:31 PM

Reply
 
Thank you I'm looking it up now!

pixellany 02-27-2007 02:48 PM

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

weibullguy 03-01-2007 12:16 PM

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.


All times are GMT -5. The time now is 04:15 PM.