sed - insert text beforecertain line (387 Views)
Reply
Super Advisor
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 1 of 10 (387 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 10 (387 Views)

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
Bob E Campbell
Posts: 764
Registered: ‎03-31-2004
Message 3 of 10 (387 Views)

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
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 4 of 10 (387 Views)

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
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 5 of 10 (387 Views)

Re: sed - insert text beforecertain line

Not date but data or text.
Jesus is the King
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 6 of 10 (387 Views)

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
Piotr Kirklewski
Posts: 339
Registered: ‎01-12-2007
Message 7 of 10 (387 Views)

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
Dennis Handly
Posts: 25,054
Registered: ‎03-06-2006
Message 8 of 10 (387 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 10 (387 Views)

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
Bob E Campbell
Posts: 764
Registered: ‎03-31-2004
Message 10 of 10 (387 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.