<?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: change 5th line after a pattern - AWK in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506449#M681114</link>
    <description>Ah, so contrary to the original question, but in line with what we all expected, the lines do have values on them already.&lt;BR /&gt;&lt;BR /&gt;I forgot to consider the awk range selector as an option to to solve this. It looks a little cleaner than maintaining flags as I suggested.&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;$  awk -v mode=TEST '/bootpd:/,/^mode/ {if (/^mode/) $0="mode = " mode} 1' x&lt;BR /&gt;/usr/sbin/too early/&lt;BR /&gt;mode =&lt;BR /&gt;/usr/sbin/bootpd:/&lt;BR /&gt;type =&lt;BR /&gt;class =&lt;BR /&gt;owner =&lt;BR /&gt;group =&lt;BR /&gt;mode = TEST&lt;BR /&gt;checksum =&lt;BR /&gt;size =&lt;BR /&gt;/usr/sbin/too late/&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
    <pubDate>Fri, 02 Oct 2009 12:22:26 GMT</pubDate>
    <dc:creator>Hein van den Heuvel</dc:creator>
    <dc:date>2009-10-02T12:22:26Z</dc:date>
    <item>
      <title>change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506440#M681105</link>
      <description>I have a requirement to assign a value "xxx" to mode in the below file.&lt;BR /&gt;&lt;BR /&gt;I am able to print the mode line (awk's default action). How can I assign this 5th line to a variable so as to change its value and then print it i.e print:&lt;BR /&gt;mode = XXX&lt;BR /&gt;&lt;BR /&gt;I am looking for an awk solution.&lt;BR /&gt;&lt;BR /&gt;file:&lt;BR /&gt;&lt;BR /&gt;/usr/sbin/bootpd:/&lt;BR /&gt;type = &lt;BR /&gt;class =&lt;BR /&gt;owner =&lt;BR /&gt;group =&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;size =&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;$ awk '!--c;/\/usr\/sbin\/bootpd:/ { c=5 }' file&lt;BR /&gt;&lt;BR /&gt;mode =&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Oct 2009 14:03:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506440#M681105</guid>
      <dc:creator>user57</dc:creator>
      <dc:date>2009-10-01T14:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506441#M681106</link>
      <description>What are you setting the "mode=" to? &lt;BR /&gt;&lt;BR /&gt;And why are you searching for the 5th line under the /usr/sbin/bootpd" pattern? Can't you search for the "mode" pattern and set it? Are there different types of "mode=" lines that are not related to the /usr/sbin/bootpd line?</description>
      <pubDate>Thu, 01 Oct 2009 14:28:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506441#M681106</guid>
      <dc:creator>TTr</dc:creator>
      <dc:date>2009-10-01T14:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506442#M681107</link>
      <description>Here is something that may be of help. Uses sed and awk. The sed is used to print the 3rd line of a file, the awk is pattern matching. For you, make the necessary changes to to print 5th line. If the pattern matches, can use sed again to substitute your desired value in its place. Make the changes you need and as always, test.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;charset=us-ascii&lt;BR /&gt;export charset&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;find /apps/sci/loaded -type f -mtime 0 -print &amp;gt; /apps/sci/loaded/scifiles&lt;BR /&gt;&lt;BR /&gt;for FILE in /apps/sci/loaded/scifiles&lt;BR /&gt;do&lt;BR /&gt; cat $FILE |\&lt;BR /&gt; while read line&lt;BR /&gt; do&lt;BR /&gt;  SCI=`sed -n 3,3p $line | awk '/046/ &amp;amp;&amp;amp; /TOWER/ {print $0}' $line`&lt;BR /&gt;  if [ "$SCI" != "" ]&lt;BR /&gt;  then&lt;BR /&gt;   if [ "$line" = "/apps/sci/loaded/scifiles" ]&lt;BR /&gt;   then&lt;BR /&gt;    echo "$line" &amp;gt; /dev/null&lt;BR /&gt;   elif [ "$line" = "/apps/sci/loaded/mailedtosci.list" ]&lt;BR /&gt;   then&lt;BR /&gt;    echo "$line" &amp;gt; /dev/null&lt;BR /&gt;   elif [ "$line" = "/apps/sci/loaded/mailsci.sh" ]&lt;BR /&gt;   then&lt;BR /&gt;    echo "$line" &amp;gt; /dev/null&lt;BR /&gt;   else&lt;BR /&gt;    mailx -s "Decoder File `basename $line`" garlric@exchange1 &amp;lt; $line&lt;BR /&gt;    echo "`basename $line` `date +'%b %d %Y %X'`" &amp;gt;&amp;gt; mailedtosci.list&lt;BR /&gt;   fi&lt;BR /&gt;  fi&lt;BR /&gt; done&lt;BR /&gt;done&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Oct 2009 15:00:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506442#M681107</guid>
      <dc:creator>Rick Garland</dc:creator>
      <dc:date>2009-10-01T15:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506443#M681108</link>
      <description>Do you always want to change the 5th line, or do you want to change the line starting with 'mode'&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;gt;&amp;gt; I am looking for an awk solution.&lt;BR /&gt;Hmm, why? Apparently you do not know awk too well, or at least not yet. Sorry. Couldn't resist!&lt;BR /&gt;&lt;BR /&gt;Anyway.... assuming you want the line with 'mode=' and assuming you want the xxx to be a variable and assuming you want to stick it back into a file, you may want to try:&lt;BR /&gt;&lt;BR /&gt;# awk -v mode=TEST '/^mode/{$0 = $0 mode} 1' x  &amp;gt; y&lt;BR /&gt;&lt;BR /&gt;This reads the file x,&lt;BR /&gt;writes to y,&lt;BR /&gt;sets up a variable called mode to value TEST.&lt;BR /&gt;The passes an awak 'one-liner'.&lt;BR /&gt;The one-liner first looks for a line starting with 'mode, and if it finds that ads the variable 'mode' to the end.&lt;BR /&gt;The next instruction is '1' which is true and causes awk to do the default thing... print $0.&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Oct 2009 15:24:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506443#M681108</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2009-10-01T15:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506444#M681109</link>
      <description>Oops, hit submit before adding the example.&lt;BR /&gt;And please clarify what you mean with 'after a pattern'. &lt;BR /&gt;Could the same line be found earlier? &lt;BR /&gt;Could the same line be found later?&lt;BR /&gt;&lt;BR /&gt;So maybe you want something along the lines of the one liner below.&lt;BR /&gt;If only starts looking for mode when a 'begin' has been set by looking for the bootpd line.&lt;BR /&gt;It stops looking when it has seen one.&lt;BR /&gt;&lt;BR /&gt;$ cat x&lt;BR /&gt;/usr/sbin/too early/&lt;BR /&gt;mode =&lt;BR /&gt;/usr/sbin/bootpd:/&lt;BR /&gt;type =&lt;BR /&gt;class =&lt;BR /&gt;owner =&lt;BR /&gt;group =&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;size =&lt;BR /&gt;/usr/sbin/too late/&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;&lt;BR /&gt;$ awk -v mode=TEST '/bootpd:/{beg++} beg &amp;amp;&amp;amp; ! end &amp;amp;&amp;amp; /^mode/ {$0 = $0 mode; end++} 1' x&lt;BR /&gt;/usr/sbin/too early/&lt;BR /&gt;mode =&lt;BR /&gt;/usr/sbin/bootpd:/&lt;BR /&gt;type =&lt;BR /&gt;class =&lt;BR /&gt;owner =&lt;BR /&gt;group =&lt;BR /&gt;mode =TEST&lt;BR /&gt;checksum =&lt;BR /&gt;size =&lt;BR /&gt;/usr/sbin/too late/&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Enjoy!&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Oct 2009 15:36:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506444#M681109</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2009-10-01T15:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506445#M681110</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Rather interesting.&lt;BR /&gt;&lt;BR /&gt;# awk '!--c {print $0,"XXX"};/\/usr\/sbin\/bootpd:/ { c=5 }' file&lt;BR /&gt;mode = XXX&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Oct 2009 15:37:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506445#M681110</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2009-10-01T15:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506446#M681111</link>
      <description>If you want to change all of the "mode=" you can use ch_rc(1m):&lt;BR /&gt;/usr/sbin/ch_rc -a -p "mode=99" file</description>
      <pubDate>Fri, 02 Oct 2009 03:25:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506446#M681111</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2009-10-02T03:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506447#M681112</link>
      <description>Thanks all. I appreciate this task is best solved using sed - and wasn't explained very well initially. The input file is  composed of many 8 line stanza's with the format:&lt;BR /&gt;&lt;BR /&gt;attribute = value&lt;BR /&gt;&lt;BR /&gt;I was merely trying to change the value of one attribute (mode) in one stanza (bootpd) leaving all other values/stanza's unchanged. &lt;BR /&gt;&lt;BR /&gt;I think there's enough in the combined response to achieve the task in awk.</description>
      <pubDate>Fri, 02 Oct 2009 07:12:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506447#M681112</guid>
      <dc:creator>user57</dc:creator>
      <dc:date>2009-10-02T07:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506448#M681113</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I was merely trying to change the value of one attribute (mode) in one stanza (bootpd) leaving all other values/stanza's unchanged. &lt;BR /&gt;&lt;BR /&gt;Then here's an alternative solution:&lt;BR /&gt;&lt;BR /&gt;# perl -pe '?/usr/sbin/bootpd:?..?mode? and s{(mode =)}{$1 "xxx"}' file&lt;BR /&gt;&lt;BR /&gt;If you want to update "in-place" simply do:&lt;BR /&gt;&lt;BR /&gt;# perl -pi.old -e '?/usr/sbin/bootpd:?..?mode? and s{(mode =)}{$1 "xxx"}' file&lt;BR /&gt;&lt;BR /&gt;...which leaves an unmodified copy of the original file as "file.old".&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 02 Oct 2009 10:48:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506448#M681113</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2009-10-02T10:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506449#M681114</link>
      <description>Ah, so contrary to the original question, but in line with what we all expected, the lines do have values on them already.&lt;BR /&gt;&lt;BR /&gt;I forgot to consider the awk range selector as an option to to solve this. It looks a little cleaner than maintaining flags as I suggested.&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;$  awk -v mode=TEST '/bootpd:/,/^mode/ {if (/^mode/) $0="mode = " mode} 1' x&lt;BR /&gt;/usr/sbin/too early/&lt;BR /&gt;mode =&lt;BR /&gt;/usr/sbin/bootpd:/&lt;BR /&gt;type =&lt;BR /&gt;class =&lt;BR /&gt;owner =&lt;BR /&gt;group =&lt;BR /&gt;mode = TEST&lt;BR /&gt;checksum =&lt;BR /&gt;size =&lt;BR /&gt;/usr/sbin/too late/&lt;BR /&gt;mode =&lt;BR /&gt;checksum =&lt;BR /&gt;&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Fri, 02 Oct 2009 12:22:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506449#M681114</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2009-10-02T12:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506450#M681115</link>
      <description>Thank you JRF for the excellent response - spot on! I thought of a relatively straight-forward approach to the problem. Changing the desired value and preserving all others:&lt;BR /&gt;&lt;BR /&gt;awk 'BEGIN { FS="\n"; RS=""; OFS="\n"; ORS="\n\n" }&lt;BR /&gt;&lt;BR /&gt;$0 !~/^\/usr\/sbin\/bootpd:/ { print }&lt;BR /&gt;&lt;BR /&gt;$1 ~/^\/usr\/sbin\/bootpd:/ {&lt;BR /&gt;$6="    mode = r-xr-xr-x"&lt;BR /&gt;print&lt;BR /&gt;} ' file&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you also Hein for the most recent post. I haven't actually tested it as I was working on an alternative solution and reviewing JRF's perl approach.</description>
      <pubDate>Fri, 02 Oct 2009 12:42:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506450#M681115</guid>
      <dc:creator>user57</dc:creator>
      <dc:date>2009-10-02T12:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: change 5th line after a pattern - AWK</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506451#M681116</link>
      <description>Just reviewed your post Hein - a neat solution.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Fri, 02 Oct 2009 12:52:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/change-5th-line-after-a-pattern-awk/m-p/4506451#M681116</guid>
      <dc:creator>user57</dc:creator>
      <dc:date>2009-10-02T12:52:20Z</dc:date>
    </item>
  </channel>
</rss>

