<?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: replace a specifk line in a file with sed or awk in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019159#M93486</link>
    <description>Hello,&lt;BR /&gt;&lt;BR /&gt;&amp;gt;  How to i use an ksh shell var with awk and sed. &lt;BR /&gt;&lt;BR /&gt;$ LN=2&lt;BR /&gt;$ awk -v lineNr=${LN} '{if (NR==lineNr) $0="my newline";print}' &amp;lt; old &amp;gt; new1&lt;BR /&gt;$ sed "${LN}s/.*/myline/" &amp;lt; old &amp;gt; new2&lt;BR /&gt;&lt;BR /&gt;PCS</description>
    <pubDate>Wed, 13 Jun 2007 08:23:22 GMT</pubDate>
    <dc:creator>spex</dc:creator>
    <dc:date>2007-06-13T08:23:22Z</dc:date>
    <item>
      <title>replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019153#M93480</link>
      <description>Hey!&lt;BR /&gt;I want to replace line nr 220 in  a file with the text "my newline" &lt;BR /&gt;How can that be done with se or awk.</description>
      <pubDate>Wed, 13 Jun 2007 07:44:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019153#M93480</guid>
      <dc:creator>Simon Jespersen</dc:creator>
      <dc:date>2007-06-13T07:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019154#M93481</link>
      <description>Hi Simon:&lt;BR /&gt;&lt;BR /&gt;# sed -e '220s/.*/my newline/' file&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 13 Jun 2007 07:49:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019154#M93481</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-13T07:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019155#M93482</link>
      <description>awk '{if (NR==220) $0="my newline";print}' old &amp;gt; new&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.</description>
      <pubDate>Wed, 13 Jun 2007 07:51:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019155#M93482</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2007-06-13T07:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019156#M93483</link>
      <description>Thanks for thje reply, but i get strange happening&lt;BR /&gt;&lt;BR /&gt;with the sed line it just behave like im doing a cat on the file&lt;BR /&gt;# sed -e '2s/.*/mynewline/' passwd_sijes&lt;BR /&gt;&lt;BR /&gt;Here im trying to replace line 2 in the file im just getting all the file directet to stdout&lt;BR /&gt;a71223:&lt;BR /&gt;        password = SyNHeFTvoiMGw&lt;BR /&gt;        lastupdate = 1135680886&lt;BR /&gt;        flags = ADMCHG&lt;BR /&gt;&lt;BR /&gt;a74040:&lt;BR /&gt;        password = SyNHeFTvoiMGw&lt;BR /&gt;        lastupdate = 1157718667&lt;BR /&gt;        flags = ADMCHG&lt;BR /&gt;&lt;BR /&gt;a05243:&lt;BR /&gt;        password = SyNHeFTvoiMGw&lt;BR /&gt;        lastupdate = 1157718669&lt;BR /&gt;        flags = ADMCHG&lt;BR /&gt;&lt;BR /&gt;a57659:&lt;BR /&gt;        password = SyNHeFTvoiMGw&lt;BR /&gt;        lastupdate = 1157718670&lt;BR /&gt;        flags = ADMCHG&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;can you se why this is happening.&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 08:01:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019156#M93483</guid>
      <dc:creator>Simon Jespersen</dc:creator>
      <dc:date>2007-06-13T08:01:56Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019157#M93484</link>
      <description>How to i use an ksh shell var with awk and sed. &lt;BR /&gt;i have the line number in an variable that i want to parse to the awk program. Se $lineNr&lt;BR /&gt;cat myfile |  awk '{if (NR==$lineNr) $0="my newline";print}' old &amp;gt; new&lt;BR /&gt;&lt;BR /&gt;rhanks</description>
      <pubDate>Wed, 13 Jun 2007 08:05:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019157#M93484</guid>
      <dc:creator>Simon Jespersen</dc:creator>
      <dc:date>2007-06-13T08:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019158#M93485</link>
      <description>Hi (again) Simon:&lt;BR /&gt;&lt;BR /&gt;You must redirect the STDOUT of 'sed' to a new file:&lt;BR /&gt;&lt;BR /&gt;# sed -e '220s/.*/my newline/' file file.new&lt;BR /&gt;# mv file.new file&lt;BR /&gt;&lt;BR /&gt;If you would like to do "inplace" updates without having to move the output back over the original file, use Perl:&lt;BR /&gt;&lt;BR /&gt;# perl -pi.old -e 's/.*/mynewline/ if $.==220' file&lt;BR /&gt;&lt;BR /&gt;This will modify line # 220, replacing it with "mynewline".  The 'file" will be modified inplace with a backup copy named 'file.old'.  If you don't want the backup, do:&lt;BR /&gt;&lt;BR /&gt;# perl -pi -e 's/.*/mynewline/ if $.==220' file&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 08:14:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019158#M93485</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-13T08:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019159#M93486</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;&amp;gt;  How to i use an ksh shell var with awk and sed. &lt;BR /&gt;&lt;BR /&gt;$ LN=2&lt;BR /&gt;$ awk -v lineNr=${LN} '{if (NR==lineNr) $0="my newline";print}' &amp;lt; old &amp;gt; new1&lt;BR /&gt;$ sed "${LN}s/.*/myline/" &amp;lt; old &amp;gt; new2&lt;BR /&gt;&lt;BR /&gt;PCS</description>
      <pubDate>Wed, 13 Jun 2007 08:23:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019159#M93486</guid>
      <dc:creator>spex</dc:creator>
      <dc:date>2007-06-13T08:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019160#M93487</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;&amp;gt; How to i use an ksh shell var with awk and sed.i have the line number in an variable that i want to parse to the awk program. Se $lineNr&lt;BR /&gt;&lt;BR /&gt;cat myfile | awk '{if (NR==$lineNr) $0="my newline";print}' old &amp;gt; new&lt;BR /&gt;&lt;BR /&gt;You pass variables to 'awk' using the '-v' switch and argument:&lt;BR /&gt;&lt;BR /&gt;# awk -v lineNr=220 '{if (NR==lineNr) $0="my newline";print}' file file.new&lt;BR /&gt;&lt;BR /&gt;*Notice* that the 'lineNr' variable does not have the sigil ($) when referenced within the 'awk' script, nor does it have it when it is declared.  However:&lt;BR /&gt;&lt;BR /&gt;# lineNR=220&lt;BR /&gt;# awk -v lineNr=${lineNr} '{if (NR==lineNr) $0="my newline";print}' file file.new&lt;BR /&gt;&lt;BR /&gt;...is necessary since you are now using a shell variable's value.&lt;BR /&gt;&lt;BR /&gt;Do not use 'cat' to open and read a file and then pipe that output to 'awk'!  That's a waste of a process.  'awk' uses the commandline arguments as filenames to process.&lt;BR /&gt;&lt;BR /&gt;For 'sed' use double quotes like this:&lt;BR /&gt;&lt;BR /&gt;# L=220&lt;BR /&gt;# sed -e "${L}s/.*/mynewline/" file &amp;gt; file.new&lt;BR /&gt;# mv file.new file&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 08:24:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019160#M93487</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-13T08:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019161#M93488</link>
      <description>cool stuff, thanks a lot. &lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 08:24:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019161#M93488</guid>
      <dc:creator>Simon Jespersen</dc:creator>
      <dc:date>2007-06-13T08:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: replace a specifk line in a file with sed or awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019162#M93489</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;one addition to JRF's sed lines:&lt;BR /&gt;Keep in mind, when putting such stuff in a script, to check the success of the commands.&lt;BR /&gt;&lt;BR /&gt;Being not root or acting via NFS or ..., you may have write access to the directory but no read access the the file or vice versa, so something like&lt;BR /&gt;# L=220&lt;BR /&gt;# sed -e "${L}s/.*/mynewline/" file &amp;gt; file.new&lt;BR /&gt;# mv file.new file&lt;BR /&gt;&lt;BR /&gt;may destroy 'file'. I suggest always&lt;BR /&gt;sed -e "${L}s/.*/mynewline/" file &amp;gt; file.new &amp;amp;&amp;amp; mv file.new file&lt;BR /&gt;&lt;BR /&gt;which else won't try a 'mv' when 'file.new' cannot be created.&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Jun 2007 05:53:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-a-specifk-line-in-a-file-with-sed-or-awk/m-p/4019162#M93489</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-06-14T05:53:20Z</dc:date>
    </item>
  </channel>
</rss>

