LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-12-2008, 03:44 PM   #1
craftereric
LQ Newbie
 
Registered: Mar 2008
Posts: 21

Rep: Reputation: 15
bash: how to insert information into the middle of text file


I have a text file like this:
Line 1
Line 2
Line 3
Line 4
JAVA_HOME=/usr/local/bin
Line 5
Line 6
Line 7
Line 8
Line 9

I need to insert a line after the JAVA_HOME line. So the new file would look like:

Line 1
Line 2
Line 3
Line 4
JAVA_HOME=/usr/local/bin
NEWLINE WAS INSERTED HERE
Line 5
Line 6
Line 7
Line 8
Line 9

My file can be any size and there is no way to know beforehand which line number is the place where I need to insert. The key is that I have to insert after finding JAVA_HOME. How can I do this automatically?
 
Old 06-12-2008, 03:57 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
sed '/^JAVA_HOME/a this is the new line' filename > newfilename

What books are you using for BASH? I recommend starting with "Bash Guide for Beginners" by Machtelt Garrels---free at tldp.org
 
Old 06-12-2008, 04:19 PM   #3
craftereric
LQ Newbie
 
Registered: Mar 2008
Posts: 21

Original Poster
Rep: Reputation: 15
it works

This works!! Thanks for the help and the reference.
 
  


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
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 12:02 PM
BASH append to middle of file baks Programming 4 02-28-2007 04:27 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 12:55 PM
insert something in the middle of file using ansi c? Thinking Programming 5 07-26-2005 02:47 PM
SH SCRIPT +pulling information from a text file chrisfirestar Linux - General 1 01-30-2004 08:14 AM


All times are GMT -5. The time now is 02:52 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