<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: sed - insert text beforecertain line in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548677#M678792</link>
    <description>&amp;gt;I was trying to redirect the output back to my file.&lt;BR /&gt;&amp;gt; sed -e '/B/i\BEFORE\' test &amp;gt; test&lt;BR /&gt;&amp;gt;And all the text inside the file is wiped out.&lt;BR /&gt;&lt;BR /&gt;You can't do that.  You must use a separate file then mv it back.&lt;BR /&gt;sed -e '/B/i\BEFORE\' test &amp;gt; test.new&lt;BR /&gt;mv test.new test&lt;BR /&gt;&lt;BR /&gt;(GNU sed has a -i option for inplace editing.)&lt;BR /&gt;</description>
    <pubDate>Sat, 12 Dec 2009 06:39:57 GMT</pubDate>
    <dc:creator>Dennis Handly</dc:creator>
    <dc:date>2009-12-12T06:39:57Z</dc:date>
    <item>
      <title>sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548670#M678785</link>
      <description>Hi there&lt;BR /&gt;I need to place a text inside many php scripts.&lt;BR /&gt;I have no problem with replacement: &lt;BR /&gt;&lt;BR /&gt;sed -i s/patern/replacement/ /file&lt;BR /&gt;&lt;BR /&gt;But now before: "$conn = mysql_connect("&lt;BR /&gt;I need to insert three lines:&lt;BR /&gt;&lt;BR /&gt;$MYSQL_PASSWORD = "xxx";&lt;BR /&gt;$MYSQL_USERNAME = "root";&lt;BR /&gt;$MYSQL_HOST="127.0.0.1";&lt;BR /&gt;&lt;BR /&gt;How can I do that ?&lt;BR /&gt;&lt;BR /&gt;Reagrds&lt;BR /&gt;&lt;BR /&gt;PEter&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Dec 2009 19:44:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548670#M678785</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2009-12-11T19:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548671#M678786</link>
      <description>Hi Peter:&lt;BR /&gt;&lt;BR /&gt;I'd use a simple Perl script:&lt;BR /&gt;&lt;BR /&gt;# cat ./filter&lt;BR /&gt;#!/usr/bin/perl -p&lt;BR /&gt;m{"\$conn = mysql_connect\("} and print &amp;lt;&lt;EOF&gt;&lt;/EOF&gt;\$MYSQL_PASSWORD = "xxx";&lt;BR /&gt;\$MYSQL_USERNAME = "root";&lt;BR /&gt;\$MYSQL_HOST="127.0.0.1";&lt;BR /&gt;EOF&lt;BR /&gt;&lt;BR /&gt;...and given a file like:&lt;BR /&gt;&lt;BR /&gt;# cat .myfile&lt;BR /&gt;This is my file&lt;BR /&gt;"$conn = mysql_connect("&lt;BR /&gt;this follows the $conn line...&lt;BR /&gt;&lt;BR /&gt;...running:&lt;BR /&gt;&lt;BR /&gt;# ./filter ./myfile&lt;BR /&gt;This is my file&lt;BR /&gt;$MYSQL_PASSWORD = "xxx";&lt;BR /&gt;$MYSQL_USERNAME = "root";&lt;BR /&gt;$MYSQL_HOST="127.0.0.1";&lt;BR /&gt;"$conn = mysql_connect("&lt;BR /&gt;this follows the $conn line...&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 11 Dec 2009 19:59:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548671#M678786</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2009-12-11T19:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548672#M678787</link>
      <description>To answer in sed:&lt;BR /&gt;&lt;BR /&gt;$ cat foo&lt;BR /&gt;A&lt;BR /&gt;B&lt;BR /&gt;C&lt;BR /&gt;D&lt;BR /&gt;$ sed -e '/B/i\&lt;BR /&gt;BEFORE\&lt;BR /&gt;Also before' foo&lt;BR /&gt;A&lt;BR /&gt;BEFORE&lt;BR /&gt;Also before&lt;BR /&gt;B&lt;BR /&gt;C&lt;BR /&gt;D&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Dec 2009 23:56:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548672#M678787</guid>
      <dc:creator>Bob E Campbell</dc:creator>
      <dc:date>2009-12-11T23:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548673#M678788</link>
      <description>Hi &lt;BR /&gt;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 ?</description>
      <pubDate>Sat, 12 Dec 2009 03:47:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548673#M678788</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2009-12-12T03:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548674#M678789</link>
      <description>Not date but data or text.</description>
      <pubDate>Sat, 12 Dec 2009 03:47:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548674#M678789</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2009-12-12T03:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548675#M678790</link>
      <description>Just redirect the output to a new file name,&lt;BR /&gt;or when opting for the perl solution opt for the -i = inplace edit option.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;# perl -pe 'program' in &amp;gt; out&lt;BR /&gt;&lt;BR /&gt;or &lt;BR /&gt;&lt;BR /&gt;# perl -p -i -e 'program in-and-out&lt;BR /&gt;&lt;BR /&gt;Try on test files first!&lt;BR /&gt;&lt;BR /&gt;Hein</description>
      <pubDate>Sat, 12 Dec 2009 03:57:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548675#M678790</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2009-12-12T03:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548676#M678791</link>
      <description>root@fog:~# cat test&lt;BR /&gt;A&lt;BR /&gt;&lt;BR /&gt;B&lt;BR /&gt;&lt;BR /&gt;C&lt;BR /&gt;&lt;BR /&gt;D&lt;BR /&gt;&lt;BR /&gt;sed -e '/B/i\BEFORE\' test &lt;BR /&gt;A&lt;BR /&gt;&lt;BR /&gt;BEFORE&lt;BR /&gt;B&lt;BR /&gt;&lt;BR /&gt;C&lt;BR /&gt;&lt;BR /&gt;D&lt;BR /&gt;&lt;BR /&gt;So it does what I want except it sends the output to the standard output.&lt;BR /&gt;I was trying to redirect the output back to my file.&lt;BR /&gt;&lt;BR /&gt;root@fog:~# sed -e '/B/i\BEFORE\' test &amp;gt; test&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;And all the text inside the file is wiped out. &lt;BR /&gt;&lt;BR /&gt;:(&lt;BR /&gt;&lt;BR /&gt;root@fog:~# cat test&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 12 Dec 2009 04:13:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548676#M678791</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2009-12-12T04:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548677#M678792</link>
      <description>&amp;gt;I was trying to redirect the output back to my file.&lt;BR /&gt;&amp;gt; sed -e '/B/i\BEFORE\' test &amp;gt; test&lt;BR /&gt;&amp;gt;And all the text inside the file is wiped out.&lt;BR /&gt;&lt;BR /&gt;You can't do that.  You must use a separate file then mv it back.&lt;BR /&gt;sed -e '/B/i\BEFORE\' test &amp;gt; test.new&lt;BR /&gt;mv test.new test&lt;BR /&gt;&lt;BR /&gt;(GNU sed has a -i option for inplace editing.)&lt;BR /&gt;</description>
      <pubDate>Sat, 12 Dec 2009 06:39:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548677#M678792</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2009-12-12T06:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548678#M678793</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I was trying to redirect the output back to my file.&lt;BR /&gt;&amp;gt; sed -e '/B/i\BEFORE\' test &amp;gt; test&lt;BR /&gt;&amp;gt; And all the text inside the file is wiped out.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;So it seems that you were using a GNU 'sed' when you said:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I have no problem with replacement:&lt;BR /&gt;&amp;gt; sed -i s/patern/replacement/ /file&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -p&lt;BR /&gt;&lt;BR /&gt;... to:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -p -i&lt;BR /&gt;&lt;BR /&gt;...to update in-place; or:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -p -i.old&lt;BR /&gt;&lt;BR /&gt;...to update in-place while preserving the original file with a ".old" suffix.  This is akin to the GNU 'sed'.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 12 Dec 2009 14:26:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548678#M678793</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2009-12-12T14:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: sed - insert text beforecertain line</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548679#M678794</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;I would just explicitly use a temp file.</description>
      <pubDate>Mon, 14 Dec 2009 19:02:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sed-insert-text-beforecertain-line/m-p/4548679#M678794</guid>
      <dc:creator>Bob E Campbell</dc:creator>
      <dc:date>2009-12-14T19:02:19Z</dc:date>
    </item>
  </channel>
</rss>

