<?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: string substituion in awk in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731530#M101070</link>
    <description>&lt;BR /&gt;Fine answer by JRF, but please also consider the awk (and perl) "Built-in Variables that Convey Information" (man page) to do exactly what you need without the extra step:&lt;BR /&gt;&lt;BR /&gt;Specific example: ENVIRON["PWD"]&lt;BR /&gt;&lt;BR /&gt;You can pick up any exported variable that way:&lt;BR /&gt;&lt;BR /&gt;# x=test&lt;BR /&gt;# export x&lt;BR /&gt;# awk 'END{print ENVIRON["x"]}' /dev/null&lt;BR /&gt;&lt;BR /&gt;btw... i would advise against export a variable named 'path'.... too confusing.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
    <pubDate>Wed, 15 Feb 2006 21:36:26 GMT</pubDate>
    <dc:creator>Hein van den Heuvel</dc:creator>
    <dc:date>2006-02-15T21:36:26Z</dc:date>
    <item>
      <title>string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731525#M101065</link>
      <description>I have the following script&lt;BR /&gt;&lt;BR /&gt;awk '/testbed/ {real=$NF; soft=$(NF-2)}\&lt;BR /&gt;{&lt;BR /&gt;  print real,soft&lt;BR /&gt; if ( real ~ /c6.4.4.x/) {&lt;BR /&gt;    newreal=s,'c6.4.4.x,new644,&lt;BR /&gt;  fi&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;I knwo the new real part is wrong..but I want to do string substituion in awk, is it possible?&lt;BR /&gt;&lt;BR /&gt;Mimosa&lt;BR /&gt;</description>
      <pubDate>Tue, 14 Feb 2006 19:17:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731525#M101065</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-02-14T19:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731526#M101066</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;See the manpages for 'awk'.  You could use the 'sub' command --- something like this:&lt;BR /&gt;&lt;BR /&gt;# cat .myawk&lt;BR /&gt;#!/usr/bin/awk -f&lt;BR /&gt;/testbed/ {real=$NF; soft=$(NF-2)}\&lt;BR /&gt;{&lt;BR /&gt;print real,soft&lt;BR /&gt;if ( real ~ /c6.4.4.x/) {&lt;BR /&gt;sub(/c6.4.4.x/,"new644",$0)&lt;BR /&gt;}&lt;BR /&gt;print&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Use like:&lt;BR /&gt;&lt;BR /&gt;# echo "testbed has a value of c6.4.4.x " | ./myawk&lt;BR /&gt;&lt;BR /&gt;...returns:&lt;BR /&gt;&lt;BR /&gt;c6.4.4.x value&lt;BR /&gt;testbed has a value of new644 &lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 14 Feb 2006 19:37:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731526#M101066</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-02-14T19:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731527#M101067</link>
      <description>&lt;BR /&gt;Why don't you give us some real (sic) sample input lines. Just a few, as a .txt attachment and as text to a next reply. Also please show the desired output.&lt;BR /&gt;For example, I am wondering whether you only want to deal with c6.4.4.x, or whether you perhaps want a generic function changing aX.Y.X.c into newXYZ. Would c1.2.3. y need to become new123?&lt;BR /&gt;If so, then you probably want to 'split' the field and glue the pieces back together.  Example for that:&lt;BR /&gt;&lt;BR /&gt;# awk '{ real=$0; split(real,array,"[^0-9]+"); new = "new" array[2] array[3] array[4]; print new}'&lt;BR /&gt;c6.4.4.x&lt;BR /&gt;new644&lt;BR /&gt;test12.345.67.xxxx&lt;BR /&gt;new1234567&lt;BR /&gt;&lt;BR /&gt;The "[^0-9]+" is a regular expression to split on, using any series of non-digits&lt;BR /&gt;The [0-9] being digits 0 thru 9&lt;BR /&gt;The ^ meaning NOT&lt;BR /&gt;and the + indicating 'one or more'&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Feb 2006 00:06:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731527#M101067</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-02-15T00:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731528#M101068</link>
      <description>both are excellent answer.&lt;BR /&gt;&lt;BR /&gt;thank you..&lt;BR /&gt;&lt;BR /&gt;btw..&lt;BR /&gt;&lt;BR /&gt;if I have something like&lt;BR /&gt;&lt;BR /&gt;path='pwd'&lt;BR /&gt;awk '{&lt;BR /&gt;  if ($path ~ 644) &lt;BR /&gt;    print "get it"&lt;BR /&gt;&lt;BR /&gt;}'&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;that doesnt work...because path was defined outside of awk...how do I use path?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Feb 2006 18:46:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731528#M101068</guid>
      <dc:creator>Gemini_2</dc:creator>
      <dc:date>2006-02-15T18:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731529#M101069</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Here's two ways (by example) to pass variables into an awk script:&lt;BR /&gt;&lt;BR /&gt;# awk -v PATH=Gemini 'BEGIN{print PATH}'&lt;BR /&gt;&lt;BR /&gt;# awk 'END{print PATH}' PATH=Gemini /dev/null&lt;BR /&gt;&lt;BR /&gt;On HP-UX either syntax will work.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 15 Feb 2006 18:59:36 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731529#M101069</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-02-15T18:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731530#M101070</link>
      <description>&lt;BR /&gt;Fine answer by JRF, but please also consider the awk (and perl) "Built-in Variables that Convey Information" (man page) to do exactly what you need without the extra step:&lt;BR /&gt;&lt;BR /&gt;Specific example: ENVIRON["PWD"]&lt;BR /&gt;&lt;BR /&gt;You can pick up any exported variable that way:&lt;BR /&gt;&lt;BR /&gt;# x=test&lt;BR /&gt;# export x&lt;BR /&gt;# awk 'END{print ENVIRON["x"]}' /dev/null&lt;BR /&gt;&lt;BR /&gt;btw... i would advise against export a variable named 'path'.... too confusing.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Feb 2006 21:36:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731530#M101070</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-02-15T21:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731531#M101071</link>
      <description>You can use variable as,&lt;BR /&gt;&lt;BR /&gt;awk -v var="information" '{ .... }'&lt;BR /&gt;&lt;BR /&gt;or&lt;BR /&gt;&lt;BR /&gt;awk '{ .... }' var="information"&lt;BR /&gt;&lt;BR /&gt;Note: You can define only one variable in second method.&lt;BR /&gt;&lt;BR /&gt;Else set a environment variable as,&lt;BR /&gt;&lt;BR /&gt;# export VAR="Information"&lt;BR /&gt;&lt;BR /&gt;Use in awk with ENVIRON["variable"] will help out.&lt;BR /&gt;&lt;BR /&gt;You can use perl as well as for this.&lt;BR /&gt;&lt;BR /&gt;--&lt;BR /&gt;Muthu</description>
      <pubDate>Thu, 16 Feb 2006 00:51:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731531#M101071</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2006-02-16T00:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731532#M101072</link>
      <description>Hi Mimosa,&lt;BR /&gt;&lt;BR /&gt;path=644&lt;BR /&gt;%echo $path|awk '{if ($0 ~ 644) print "get it"}'&lt;BR /&gt;get it&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;HTH,&lt;BR /&gt;Art&lt;BR /&gt;</description>
      <pubDate>Thu, 16 Feb 2006 04:54:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731532#M101072</guid>
      <dc:creator>Arturo Galbiati</dc:creator>
      <dc:date>2006-02-16T04:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731533#M101073</link>
      <description>Muthu, why would anyone come in *hours later* and repeat the exact same advice others have given? A million apologizes if that reply sat in a buffer for too long and just 'submitted' late... I know I have done that. But if it is just a re-hash stating something much similar to prior replies, then what is the added value in that?&lt;BR /&gt;What you do with your time is of course none of my concern. However, it does create clutter and is does bring a topic which I was 'following' back to the top, making me waste my time.&lt;BR /&gt;&lt;BR /&gt;Sorry to 'pick' on you, it is not really personal as this happens time and again with mutliple contributors. I do appreciate your contributions in general!&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 16 Feb 2006 07:24:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731533#M101073</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-02-16T07:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: string substituion in awk</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731534#M101074</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;Hein's point regarding gaining the values of (ENV)ironmental variables directly is the more appropriate choice.&lt;BR /&gt;&lt;BR /&gt;For the record, it *is* indeed possible to pass multiple variable values into an awk script despite what a post above would suggest:&lt;BR /&gt;&lt;BR /&gt;# awk 'END{print "I am",FIRST,"and I am",LAST}' FIRST=alpha LAST=omega /dev/null &lt;BR /&gt;&lt;BR /&gt;# awk -v FIRST=alpha -v LAST=omega 'END{print "I am",FIRST,"and I am",LAST}' /dev/null &lt;BR /&gt;&lt;BR /&gt;*Either* form above will produce the output:&lt;BR /&gt;&lt;BR /&gt;I am alpha and I am omega&lt;BR /&gt;&lt;BR /&gt;ALSO: Your choice of the two methods I suggested is also influenced when/if you use BEGIN and END blocks in 'awk'.&lt;BR /&gt;&lt;BR /&gt;# awk 'BEGIN{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts&lt;BR /&gt;&lt;BR /&gt;...prints:&lt;BR /&gt;&lt;BR /&gt;I am  and I am &lt;BR /&gt;ok&lt;BR /&gt;&lt;BR /&gt;...whereas:&lt;BR /&gt;&lt;BR /&gt;# awk 'END{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts&lt;BR /&gt;&lt;BR /&gt;...prints:&lt;BR /&gt;&lt;BR /&gt;I am alpha and I am omega&lt;BR /&gt;ok&lt;BR /&gt;&lt;BR /&gt;Yet, either:&lt;BR /&gt;&lt;BR /&gt;# awk -v FIRST=alpha -v LAST=omega 'END{print ...&lt;BR /&gt;&lt;BR /&gt;(or)&lt;BR /&gt;&lt;BR /&gt;# awk -v FIRST=alpha -v LAST=omega 'BEGIN{print ...&lt;BR /&gt;&lt;BR /&gt;...produces the desired:&lt;BR /&gt;&lt;BR /&gt;I am alpha and I am omega&lt;BR /&gt;ok&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 16 Feb 2006 12:58:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-substituion-in-awk/m-p/3731534#M101074</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-02-16T12:58:52Z</dc:date>
    </item>
  </channel>
</rss>

