LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2009, 03:35 PM   #1
Carlos2dub
LQ Newbie
 
Registered: Aug 2007
Location: Madrid
Distribution: Debian 4, openSUSE 10.3, RHEL 5
Posts: 24

Rep: Reputation: 15
find and append


Hi all,

Im quite new scripting and I was trying to run a script which searchs for a word in a file, and appends a new line and some content after it. I found out grep+sed would be my tools but Im not getting much. I need to modify the file after the command finishes but I dont know how to do that.

I have tried:

grep word file.txt | sed a'otherword'

Many thanks,

Carlos.
 
Old 03-10-2009, 05:12 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,712

Rep: Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747
Code:
sed -i s/searchword/"searchword\nthis is a new line\nanythingelse"/
sed will edit the file -i that is inplace. Be sure you have a backup. Or do:
Code:
cat thisfile | sed s/searchword/"searchword\nthis is a new line\nanythingelse"/ > newfile
If you add 'g' after the last '/' in the sed command, sed will replace all occurences of searchword with the new sentences.

jlinkels
 
Old 03-10-2009, 08:48 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
And you can actually do it entirely in sed
Code:
sed -i filename -e /searchword/a'otherword'
The 'sed' piece is the command.
The '-i' means to edit the file in place (so as mentioned, if you're not sure - keep a backup)
The filename is the name of the file
The -e means that what follows is the sed expression
The /searchword/ part tells sed to search for lines that contain searchword and only change those.
The a'otherword' part tells sed to append otherword to the lines containing searchword.
 
Old 03-10-2009, 08:52 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Hang on, I might have misunderstood your problem. My solution will append the extra stuff at the end of the line containing the searched-for string. If you wanted to insert it right where the searched for string was (although it may be mid-line), then definitely go with jlinkel's solution.
 
Old 03-11-2009, 04:48 AM   #5
Carlos2dub
LQ Newbie
 
Registered: Aug 2007
Location: Madrid
Distribution: Debian 4, openSUSE 10.3, RHEL 5
Posts: 24

Original Poster
Rep: Reputation: 15
Many thanks to both of you, it really helped me. I think it was more accurate for my case taking patricks solution because it adds the line right below the line I search for. Now I know better how to proceed with this command.
 
  


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
bash - find and append to each file found djgerbavore Linux - General 5 11-12-2008 11:14 AM
Append files tekmann33 Linux - Newbie 6 03-10-2008 11:41 AM
how to append stderr? mathfeel Linux - General 3 06-16-2006 02:05 AM
sk_buff append new shrishailnk Linux - Networking 0 04-09-2006 02:07 AM
append to file d-rez Linux - Newbie 2 06-20-2002 05:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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