<?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 formatting in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190692#M91303</link>
    <description>Hi:&lt;BR /&gt;&lt;BR /&gt;And for completeness in the discussion, 'printf' is also intrinsic to Perl.  So you can write in C, shell, awk or Perl as best suits your needs and those of your intended maintainers.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Fri, 02 May 2008 10:42:00 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2008-05-02T10:42:00Z</dc:date>
    <item>
      <title>String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190688#M91299</link>
      <description>LIke sprintf in 'C' is there any option available in UNIX shell scripting&lt;BR /&gt;&lt;BR /&gt;For eg)&lt;BR /&gt;awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}'&lt;BR /&gt;&lt;BR /&gt;I tried to the above and got the error message.&lt;BR /&gt;&lt;BR /&gt;Is there any other way to format strings so that my purpose of writing the whole string into the file will be successful.&lt;BR /&gt;Please help&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;rkraj&lt;BR /&gt;</description>
      <pubDate>Fri, 02 May 2008 02:36:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190688#M91299</guid>
      <dc:creator>rkraj</dc:creator>
      <dc:date>2008-05-02T02:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190689#M91300</link>
      <description>&lt;!--!*#--&gt;&amp;gt; [...] UNIX [...]&lt;BR /&gt;&lt;BR /&gt;"UNIX" is not a very well-defined quantity.&lt;BR /&gt;&lt;BR /&gt;    man printf&lt;BR /&gt;&lt;BR /&gt;td192&amp;gt; printf "&amp;gt;%10s&amp;lt; &amp;gt;%-10s&amp;lt;\n" 'aaa' 'bbb'&lt;BR /&gt;&amp;gt;       aaa&amp;lt; &amp;gt;bbb       &amp;lt;</description>
      <pubDate>Fri, 02 May 2008 03:07:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190689#M91300</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2008-05-02T03:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190690#M91301</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;there is a 'printf' binary, which you can use in shell scripts, like:&lt;BR /&gt;printf "%02d %32s\n" $i $text&lt;BR /&gt;&lt;BR /&gt;In awk, your syntax needs modification: drop the  "$" before variable names.&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;</description>
      <pubDate>Fri, 02 May 2008 07:46:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190690#M91301</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2008-05-02T07:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190691#M91302</link>
      <description>As Steven said, there is printf(1).&lt;BR /&gt;If you still want to go the awk route, you need to know how to pass variables.  (It would be helpful if you printed the error message.)&lt;BR /&gt;&lt;BR /&gt;Your issue is that you have $var1 within single quotes, where variables aren't expanded and in awk $var1 basically prints field ($) var1, where var1 is likely 0.&lt;BR /&gt;&lt;BR /&gt;To pass variables, you either need to do stuttering quoting or:&lt;BR /&gt;awk -v var1="$var1" -v var2="$var2" ... '&lt;BR /&gt;BEGIN {&lt;BR /&gt;printf "%s ....\n", var1, var2, var3, var4, var5&lt;BR /&gt;exit&lt;BR /&gt;} '</description>
      <pubDate>Fri, 02 May 2008 08:05:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190691#M91302</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-05-02T08:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190692#M91303</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;And for completeness in the discussion, 'printf' is also intrinsic to Perl.  So you can write in C, shell, awk or Perl as best suits your needs and those of your intended maintainers.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 02 May 2008 10:42:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190692#M91303</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-05-02T10:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190693#M91304</link>
      <description>I am supposed to build it only on shell scripting but not on Perl.&lt;BR /&gt;&lt;BR /&gt;Also i tried this way&lt;BR /&gt;var1="CTCT 0001000Y"&lt;BR /&gt;&lt;BR /&gt; printf '%-50s' $var1&lt;BR /&gt;CTCT                                              0001000Y                                          $&lt;BR /&gt;&lt;BR /&gt;A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.&lt;BR /&gt;if var1="CTCT" then it works fine as expected.&lt;BR /&gt;&lt;BR /&gt;Any clues&lt;BR /&gt;let me know &lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;rkraj&lt;BR /&gt;</description>
      <pubDate>Fri, 02 May 2008 14:58:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190693#M91304</guid>
      <dc:creator>rkraj</dc:creator>
      <dc:date>2008-05-02T14:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190694#M91305</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I think, it is all done correctly: You are using an unquoted variable, so contained whitespace leads to mutliple arguments of your command.&lt;BR /&gt;Look at the difference of your&lt;BR /&gt;var1="CTCT 0001000Y"&lt;BR /&gt;printf '%-50s\n' $var1&lt;BR /&gt;&lt;BR /&gt;to a double quoted version&lt;BR /&gt;printf '%-50s\n' "$var1"&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Fri, 02 May 2008 15:09:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190694#M91305</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2008-05-02T15:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: String formatting</title>
      <link>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190695#M91306</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Also i tried this way&lt;BR /&gt;&amp;gt; var1="CTCT 0001000Y"&lt;BR /&gt;&lt;BR /&gt;&amp;gt; printf '%-50s' $var1&lt;BR /&gt;&amp;gt; CTCT 0001000Y $&lt;BR /&gt;&lt;BR /&gt;&amp;gt; A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.&lt;BR /&gt;if var1="CTCT" then it works fine as expected.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Any clues&lt;BR /&gt;&lt;BR /&gt;I'm not sure what it is you are asking.&lt;BR /&gt;&lt;BR /&gt;First, you probably want to include a newline when you print.  Second, if your variable has embedded spaces, but you want to treat that as *one* argument to 'printf', then you need to quote the variable argument:&lt;BR /&gt;&lt;BR /&gt;# printf '%-50s\n' "$var1"&lt;BR /&gt;CTCT 0001000Y&lt;BR /&gt;&lt;BR /&gt;Otherwise, something like this:&lt;BR /&gt;&lt;BR /&gt;# printf '%-50s\n' $var1 and yet more here&lt;BR /&gt;&lt;BR /&gt;...becomes:&lt;BR /&gt;&lt;BR /&gt;CTCT&lt;BR /&gt;0001000Y&lt;BR /&gt;and&lt;BR /&gt;yet&lt;BR /&gt;more&lt;BR /&gt;here&lt;BR /&gt;&lt;BR /&gt;...instead of:&lt;BR /&gt;&lt;BR /&gt;# printf '%-50s\n' "$var1 and yet more here"&lt;BR /&gt;CTCT 0001000Y and yet more here&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Fri, 02 May 2008 15:11:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/string-formatting/m-p/4190695#M91306</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-05-02T15:11:39Z</dc:date>
    </item>
  </channel>
</rss>

