Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

sed - insert text beforecertain line

Super Advisor

sed - insert text beforecertain line

Hi there
I need to place a text inside many php scripts.
I have no problem with replacement:

sed -i s/patern/replacement/ /file

But now before: "$conn = mysql_connect("
I need to insert three lines:

$MYSQL_PASSWORD = "xxx";
$MYSQL_USERNAME = "root";
$MYSQL_HOST="127.0.0.1";

How can I do that ?

Reagrds

PEter






Jesus is the King
9 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: sed - insert text beforecertain line

Hi Peter:

I'd use a simple Perl script:

# cat ./filter
#!/usr/bin/perl -p
m{"\$conn = mysql_connect\("} and print <\$MYSQL_PASSWORD = "xxx";
\$MYSQL_USERNAME = "root";
\$MYSQL_HOST="127.0.0.1";
EOF

...and given a file like:

# cat .myfile
This is my file
"$conn = mysql_connect("
this follows the $conn line...

...running:

# ./filter ./myfile
This is my file
$MYSQL_PASSWORD = "xxx";
$MYSQL_USERNAME = "root";
$MYSQL_HOST="127.0.0.1";
"$conn = mysql_connect("
this follows the $conn line...

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: sed - insert text beforecertain line

To answer in sed:

$ cat foo
A
B
C
D
$ sed -e '/B/i\
BEFORE\
Also before' foo
A
BEFORE
Also before
B
C
D
Super Advisor

Re: sed - insert text beforecertain line

Hi
Your solution seams to be working but the date is only being displayed on the screen - doesn't go to the file. How to accomplish that ?
Jesus is the King
Super Advisor

Re: sed - insert text beforecertain line

Not date but data or text.
Jesus is the King
Honored Contributor Honored Contributor

Re: sed - insert text beforecertain line

Just redirect the output to a new file name,
or when opting for the perl solution opt for the -i = inplace edit option.


# perl -pe 'program' in > out

or

# perl -p -i -e 'program in-and-out

Try on test files first!

Hein
Super Advisor

Re: sed - insert text beforecertain line

root@fog:~# cat test
A

B

C

D

sed -e '/B/i\BEFORE\' test
A

BEFORE
B

C

D

So it does what I want except it sends the output to the standard output.
I was trying to redirect the output back to my file.

root@fog:~# sed -e '/B/i\BEFORE\' test > test


And all the text inside the file is wiped out.

:(

root@fog:~# cat test


Jesus is the King
Acclaimed Contributor Acclaimed Contributor

Re: sed - insert text beforecertain line

>I was trying to redirect the output back to my file.
> sed -e '/B/i\BEFORE\' test > test
>And all the text inside the file is wiped out.

You can't do that. You must use a separate file then mv it back.
sed -e '/B/i\BEFORE\' test > test.new
mv test.new test

(GNU sed has a -i option for inplace editing.)
Acclaimed Contributor Acclaimed Contributor

Re: sed - insert text beforecertain line

Hi (again):

> I was trying to redirect the output back to my file.
> sed -e '/B/i\BEFORE\' test > test
> And all the text inside the file is wiped out.

As Dennis noted, you can't do that! I'm surprised you thought you could. The redirection occurs first, and hence the input file is immediately truncated. You must redirect to a new file; perform the 'sed' and then move the new file (output) over the old file (input) replacing the old file with its modified copy.

So it seems that you were using a GNU 'sed' when you said:

> I have no problem with replacement:
> sed -i s/patern/replacement/ /file

HP-UX's 'sed' doesn't offer that ability. As Hein noted, you could have added the '-i' option to my Perl script to perform an in-place update. In fact, you could change the first line of my script from:

#!/usr/bin/perl -p

... to:

#!/usr/bin/perl -p -i

...to update in-place; or:

#!/usr/bin/perl -p -i.old

...to update in-place while preserving the original file with a ".old" suffix. This is akin to the GNU 'sed'.

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: sed - insert text beforecertain line

My apologies, I did not consider the file manipulation an issue to be discussed. I thought that the sed syntax of needing newlines was the important part.

I would just explicitly use a temp file.