LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash: how to insert information into the middle of text file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-how-to-insert-information-into-the-middle-of-text-file-648891/)

craftereric 06-12-2008 02:44 PM

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?

pixellany 06-12-2008 02:57 PM

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

craftereric 06-12-2008 03:19 PM

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


All times are GMT -5. The time now is 02:33 PM.