Operating System - Linux
1827319 Members
4745 Online
109961 Solutions
New Discussion

insert text between lines

 
Piotr Kirklewski
Super Advisor

insert text between lines

Hi there
I need to know how to insert a text into existing file, just before certain text.
Example:

echo "my text" >> myfile.conf


cat myfile.conf

line 1
line 2

my text

#
# AMAVISD-NEW
#



Regards
Peter




Jesus is the King
3 REPLIES 3
Steven Schweda
Honored Contributor

Re: insert text between lines

Not a very useful description of what you
wish to do. Should "my text" be added at the
beginning of an existing line? On a new line
of its own? Is "my text" always only one
line of text (a constant or variable), or
might it be the contents of some other file?

What is the "certain text" in this "Example"?
Is it the whole line, or some part of a line?

"sed" can do some useful things. For
example:

dyi # cat text.txt
abc def
ghi jkl
mno pqr
stu vwx

dyi # sed -n -e '1,/hi/p' text.txt
abc def
ghi jkl

dyi # sed -n -e '1,/hi/d' -e p text.txt
mno pqr
stu vwx


man sed
Piotr Kirklewski
Super Advisor

Re: insert text between lines

Should "my text" be added at the
beginning of an existing line?

Yes,just before:
#
# AMAVISD-NEW
#

Is "my text" always only one
line of text (a constant or variable), or
might it be the contents of some other file?

It's just one line - constant.
Jesus is the King
Goran Koruga
Honored Contributor

Re: insert text between lines

Hello.

You can use 'sed', namely the 'i\' and 'a\' (GNU extensions) commands.

Regards,
Goran