- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sed - insert text beforecertain line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 11:44 AM
тАО12-11-2009 11:44 AM
sed - insert text beforecertain line
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 11:59 AM
тАО12-11-2009 11:59 AM
Re: sed - insert text beforecertain line
I'd use a simple Perl script:
# cat ./filter
#!/usr/bin/perl -p
m{"\$conn = mysql_connect\("} and print <
\$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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 03:56 PM
тАО12-11-2009 03:56 PM
Re: sed - insert text beforecertain line
$ cat foo
A
B
C
D
$ sed -e '/B/i\
BEFORE\
Also before' foo
A
BEFORE
Also before
B
C
D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 07:47 PM
тАО12-11-2009 07:47 PM
Re: sed - insert text beforecertain line
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 07:47 PM
тАО12-11-2009 07:47 PM
Re: sed - insert text beforecertain line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 07:57 PM
тАО12-11-2009 07:57 PM
Re: sed - insert text beforecertain line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 08:13 PM
тАО12-11-2009 08:13 PM
Re: sed - insert text beforecertain line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-11-2009 10:39 PM
тАО12-11-2009 10:39 PM
Re: sed - insert text beforecertain line
> 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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-12-2009 06:26 AM
тАО12-12-2009 06:26 AM
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.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2009 11:02 AM
тАО12-14-2009 11:02 AM
Re: sed - insert text beforecertain line
I would just explicitly use a temp file.