<?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 string in place..retain original line. in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104576#M91146</link>
    <description>Thanks James.&lt;BR /&gt;&lt;BR /&gt;You must have got a 1000 points from me this week.&lt;BR /&gt;&lt;BR /&gt;Hope I can return the favour one day!&lt;BR /&gt;&lt;BR /&gt;Cheers</description>
    <pubDate>Tue, 22 Apr 2008 13:05:00 GMT</pubDate>
    <dc:creator>OFC_EDM</dc:creator>
    <dc:date>2008-04-22T13:05:00Z</dc:date>
    <item>
      <title>Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104564#M91134</link>
      <description>This question stems from a solution I got from James R Ferguson.  Thanks James!&lt;BR /&gt;&lt;BR /&gt;Where the following command replaces a string and backs up the file before the update.&lt;BR /&gt;&lt;BR /&gt;perl -pi.old -e 's/\bDISABLETIME=\d\d\b/DISABLETIME=60/' ./login&lt;BR /&gt;&lt;BR /&gt;I want to customize this further:&lt;BR /&gt;&lt;BR /&gt;In the login file the line may be&lt;BR /&gt;#DISABLETIME=20&lt;BR /&gt;#DISABLETIME=100&lt;BR /&gt;#DISABLETIME=2000&lt;BR /&gt;&lt;BR /&gt;If I find any one or other variations I want to leave the commented out line.&lt;BR /&gt;&lt;BR /&gt;Then add a new line&lt;BR /&gt;DISABLETIME=60&lt;BR /&gt;after the original line.  &lt;BR /&gt;&lt;BR /&gt;Thereby leaving the original value in place in case I need to go back to it.&lt;BR /&gt;&lt;BR /&gt;So it may look like:&lt;BR /&gt;#DISABLETIME=200&lt;BR /&gt;DISABLETIME=60&lt;BR /&gt;After the update is done.&lt;BR /&gt;&lt;BR /&gt;How can the perl command be modified to do this?&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;</description>
      <pubDate>Mon, 21 Apr 2008 08:45:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104564#M91134</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-21T08:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104565#M91135</link>
      <description>I figured out part of this&lt;BR /&gt;&lt;BR /&gt;perl -pi.old -e 's/\bDISABLETIME=[0-9]*\b/\nDISABLETIME=60/g' ./login.test&lt;BR /&gt;&lt;BR /&gt;Will put in the newline and my new value of DISABLETIME=60.&lt;BR /&gt;&lt;BR /&gt;But it won't retain the original line.&lt;BR /&gt;&lt;BR /&gt;Maybe there's a way to reference the original string in the replacement string?</description>
      <pubDate>Mon, 21 Apr 2008 09:01:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104565#M91135</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-21T09:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104566#M91136</link>
      <description>&amp;gt;In the login file the line may be&lt;BR /&gt;&lt;BR /&gt;These lines start with "#"?&lt;BR /&gt;&lt;BR /&gt;&amp;gt;If I find any one or other variations I want to leave the commented out line.&lt;BR /&gt;&amp;gt;Thereby leaving the original value in place in case I need to go back to it.&lt;BR /&gt;&lt;BR /&gt;You need to provide more details.  Are the lines already commended out or do you want to comment them out, then add the "=60" line?&lt;BR /&gt;Do you only have the one?&lt;BR /&gt;&lt;BR /&gt;You might be able to use sed but it is easier to use awk:&lt;BR /&gt;$ awk '&lt;BR /&gt;/DISABLETIME=/ {&lt;BR /&gt;print $0&lt;BR /&gt;print "DISABLETIME=60"&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;print $0&lt;BR /&gt;}' login &amp;gt; login.new</description>
      <pubDate>Mon, 21 Apr 2008 09:13:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104566#M91136</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-04-21T09:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104567#M91137</link>
      <description>I'm updating over 100 servers.&lt;BR /&gt;&lt;BR /&gt;Some already have the DISABLETIME=60 in place and some won't.&lt;BR /&gt;&lt;BR /&gt;Some will already have it commented out.  And I won't know the value after the = sign.&lt;BR /&gt;&lt;BR /&gt;So I have to handle both those conditions:&lt;BR /&gt;Regardless I want the original line to end up being commented out.&lt;BR /&gt;&lt;BR /&gt;And append the new DISABLETIME=60 line immediately after the original&lt;BR /&gt;&lt;BR /&gt;I want to try and stick with the perl statement as it automatically backs up the file.&lt;BR /&gt;&lt;BR /&gt;Another reason is I'm trying to convert myself to Perl :)</description>
      <pubDate>Mon, 21 Apr 2008 09:24:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104567#M91137</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-21T09:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104568#M91138</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;# perl -ni.old -e 'print;if m/#\s*DISABLETIME\s*=\s*\d+/) {print "DISABLETIME=60\n"}' file&lt;BR /&gt;&lt;BR /&gt;...which would handle patterns like:&lt;BR /&gt;&lt;BR /&gt;#DISABLETIME=20&lt;BR /&gt;&lt;BR /&gt;# DISABLETIME = 2000&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 21 Apr 2008 11:01:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104568#M91138</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-21T11:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104569#M91139</link>
      <description>Thanks James,&lt;BR /&gt;&lt;BR /&gt;Did I put the code in incorrectly?  I get the error below.&lt;BR /&gt;&lt;BR /&gt;perl -ni.old -e 'print;if m/#\s*DISABLETIME\s*=\s*\d+/) {print "DISABLETIME=60\n"}' ./login.test&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;syntax error at -e line 1, near "if m/#\s*DISABLETIME\s*=\s*\d+/"&lt;BR /&gt;syntax error at -e line 1, near ";}"&lt;BR /&gt;Execution of -e aborted due to compilation errors.&lt;BR /&gt;&lt;BR /&gt;Would you mind explaining how the command works?&lt;BR /&gt;&lt;BR /&gt;I think the \s* matches all white space from the next to the end of the line (before newline character).&lt;BR /&gt;&lt;BR /&gt;And \d+ is one or more digits.&lt;BR /&gt;&lt;BR /&gt;Whats the m at the beginning?&lt;BR /&gt;&lt;BR /&gt;Am I interpreting the command correctly as follows?:&lt;BR /&gt;print the line, match the string, apply condition and then print if condition met?&lt;BR /&gt;&lt;BR /&gt;Thanks again</description>
      <pubDate>Mon, 21 Apr 2008 11:10:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104569#M91139</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-21T11:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104570#M91140</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;Sorry, mMy error; I dropped the opening parenthesis:&lt;BR /&gt;&lt;BR /&gt;# perl -ni.old -e 'print;if (m/#\s*DISABLETIME\s*=\s*\d+/) {print "DISABLETIME=60\n"}' ./login.test&lt;BR /&gt;&lt;BR /&gt;The '\s*' means zero or more whitespace (blank, tab, newline and carriage-return) characters.&lt;BR /&gt;&lt;BR /&gt;The '\d+' means one or more digits.  Note the '+' versus the '*' for one-or-more versus zero-or-more.&lt;BR /&gt;&lt;BR /&gt;The 'm' in this context is optional, but means "match".  With a forward slash '/' as a delimiter, 'm' is inferred.  Other delimiters can be used for clarity and that's when the leading 'm' is really needed.  I could have written:&lt;BR /&gt;&lt;BR /&gt;# perl -ni.old -e 'print;if (m{#\s*DISABLETIME\s*=\s*\d+}) {print "DISABLETIME=60\n"}' ./login.test&lt;BR /&gt;&lt;BR /&gt;...that is, used '{' and '}' as the matching delimiters.  This is very useful when you want to avoid the "leaning toothpick syndrome", like:&lt;BR /&gt;&lt;BR /&gt;# if ( m/\/usr\/bin\/sh/ ) ...&lt;BR /&gt;&lt;BR /&gt;...becomes:&lt;BR /&gt;&lt;BR /&gt;# if ( m{/usr/bin/sh} ) ...&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Mon, 21 Apr 2008 11:31:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104570#M91140</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-21T11:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104571#M91141</link>
      <description>&lt;!--!*#--&gt;James,&lt;BR /&gt;&lt;BR /&gt;I've hit a snag where some login files for some systems have previous commented out values.  &lt;BR /&gt;&lt;BR /&gt;Which results in multiple line matches.&lt;BR /&gt;&lt;BR /&gt;I just want to insert my DISABLETIME=60 after the LAST match&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;&lt;BR /&gt;# DISABLETIME=20&lt;BR /&gt;# DISABLETIME=100&lt;BR /&gt;DISABLETIME=60    &amp;lt;---Insert here&lt;BR /&gt;&lt;BR /&gt;Unfortunately my code does this:&lt;BR /&gt;# DISABLETIME=20&lt;BR /&gt;DISABLETIME=60    &amp;lt;---Inserts here&lt;BR /&gt;# DISABLETIME=100&lt;BR /&gt;DISABLETIME=60    &amp;lt;---and Inserts here&lt;BR /&gt;&lt;BR /&gt;I run the following:&lt;BR /&gt;  # Find any lines uncommented and comment out&lt;BR /&gt;  perl -pi.old -e 's/^DISABLETIME=\s*/#DISABLETIME=/g' ${dwnld_dir}/login.${TARGET}&lt;BR /&gt;&lt;BR /&gt;  # Find LAST commented line and insert new line&lt;BR /&gt;  perl -ni -e 'print;if (m/#\s*DISABLETIME\s*=\s*\d+/) {print "DISABLETIME=60\n"}' ${dwnld_dir}/login.${TARGET}&lt;BR /&gt;&lt;BR /&gt;Any ideas on how to improve the code?</description>
      <pubDate>Tue, 22 Apr 2008 09:48:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104571#M91141</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-22T09:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104572#M91142</link>
      <description>&lt;!--!*#--&gt;Hi (again):&lt;BR /&gt;&lt;BR /&gt;Ok, given a configuration file like this:&lt;BR /&gt;&lt;BR /&gt;# cat ./myconfig&lt;BR /&gt;this is a some config file...&lt;BR /&gt;# DISABLETIME=20&lt;BR /&gt;  # DISABLETIME= 200&lt;BR /&gt;  #DISABLETIME=1000&lt;BR /&gt;DISABLETIME=60&lt;BR /&gt;this is more of the file&lt;BR /&gt;and this is still more of the file&lt;BR /&gt;&lt;BR /&gt;...Use this script:&lt;BR /&gt;&lt;BR /&gt;# cat ./myfilter&lt;BR /&gt;#!/usr/bin/perl -i.old&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my $lines = do { local $/ = undef; &amp;lt;&amp;gt; };&lt;BR /&gt;$lines =~ s/( \#\s*DISABLETIME\s*=\d+.+?$ ) (?! \s*\#\s*DISABLETIME ) /$1\nDISABLETIME=60/smx;&lt;BR /&gt;print $lines;&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...and thus:&lt;BR /&gt;&lt;BR /&gt;# ./myfilter ./myconfig&lt;BR /&gt;&lt;BR /&gt;...yields a modified './myconfig' [with a backup as '*.old'] of:&lt;BR /&gt;&lt;BR /&gt;this is a some config file...&lt;BR /&gt;# DISABLETIME=20&lt;BR /&gt;  # DISABLETIME= 200&lt;BR /&gt;  #DISABLETIME=1000&lt;BR /&gt;DISABLETIME=60&lt;BR /&gt;this is more of the file&lt;BR /&gt;and this is still more of the file&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 22 Apr 2008 11:52:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104572#M91142</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-22T11:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104573#M91143</link>
      <description>Thanks (again) James.&lt;BR /&gt;&lt;BR /&gt;But I think I have a perl issue.&lt;BR /&gt;I try to run the code and get this.&lt;BR /&gt;I assume it means I don't have a module called warnings.pm on the system.&lt;BR /&gt;And I can't update the perl install there.&lt;BR /&gt;&lt;BR /&gt; =&amp;gt;./myfilter ./retrievedfiles/login.iecs&lt;BR /&gt;Can't locate warnings.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at ./myfilter line 3.&lt;BR /&gt;BEGIN failed--compilation aborted at ./myfilter line 3.</description>
      <pubDate>Tue, 22 Apr 2008 12:19:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104573#M91143</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-22T12:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104574#M91144</link>
      <description>I took out the &lt;BR /&gt;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;and it works</description>
      <pubDate>Tue, 22 Apr 2008 12:21:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104574#M91144</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-22T12:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104575#M91145</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;&amp;gt; But I think I have a perl issue...&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Can't locate warnings.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 ...&lt;BR /&gt;&lt;BR /&gt;Yup, you REALLY NEED a newer version of Perl.  The 'warnings' pragma appeared in the core of 5.8.&lt;BR /&gt;&lt;BR /&gt;Perl 5.8.8 would be excellent and Perl 5.10 has been out for a few months.&lt;BR /&gt;&lt;BR /&gt;In the meantime, if you don't have the 'warnings' pragma, use the '-w' switch.  It is not a robust but is far, far better than nothing!&lt;BR /&gt;&lt;BR /&gt;# perl -w&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 22 Apr 2008 12:36:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104575#M91145</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-22T12:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104576#M91146</link>
      <description>Thanks James.&lt;BR /&gt;&lt;BR /&gt;You must have got a 1000 points from me this week.&lt;BR /&gt;&lt;BR /&gt;Hope I can return the favour one day!&lt;BR /&gt;&lt;BR /&gt;Cheers</description>
      <pubDate>Tue, 22 Apr 2008 13:05:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104576#M91146</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-22T13:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Replace string in place..retain original line.</title>
      <link>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104577#M91147</link>
      <description>See above for solution</description>
      <pubDate>Tue, 22 Apr 2008 13:05:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/replace-string-in-place-retain-original-line/m-p/5104577#M91147</guid>
      <dc:creator>OFC_EDM</dc:creator>
      <dc:date>2008-04-22T13:05:45Z</dc:date>
    </item>
  </channel>
</rss>

