printf to pad with spaces BUT trips over when data being padded contains spaces

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Dundonal » Sat, 07 Jan 2006 23:11:27



I am using printf through awk to pad with spaces a variable's data to
the deired length, for example:

echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

This works great, and pads data up to 20 spaces if it isn't already.
However, if $variable contains data such as "hello world", i.e with a
space already with the data between the two words, the result is "hello
              " rather than "hello world         ".

Any ideas how to get around this?

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Greg Andre » Sun, 08 Jan 2006 00:08:19



>I am using printf through awk to pad with spaces a variable's data to
>the deired length, for example:

>echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

>This works great, and pads data up to 20 spaces if it isn't already.
>However, if $variable contains data such as "hello world", i.e with a
>space already with the data between the two words, the result is "hello
>              " rather than "hello world         ".

>Any ideas how to get around this?

Yes:

  echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

Awk splits its input line into separate fields, with the
separator being spaces and/or tabs.  So in your example
input $1 became "hello" and $2 became "world", but you're
only telling awk to give the first field ($1) to printf.

You seem to want awk to give the entire line to printf, so
you want to specify $0.

  -Greg
--

     I have a map of the United States that's actual size.
                                -- Steven Wright

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Dundonal » Sun, 08 Jan 2006 00:17:43




> >I am using printf through awk to pad with spaces a variable's data to
> >the deired length, for example:

> >echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

> >This works great, and pads data up to 20 spaces if it isn't already.
> >However, if $variable contains data such as "hello world", i.e with a
> >space already with the data between the two words, the result is "hello
> >              " rather than "hello world         ".

> >Any ideas how to get around this?

> Yes:

>   echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

> Awk splits its input line into separate fields, with the
> separator being spaces and/or tabs.  So in your example
> input $1 became "hello" and $2 became "world", but you're
> only telling awk to give the first field ($1) to printf.

> You seem to want awk to give the entire line to printf, so
> you want to specify $0.

Greg, top man, thanks very much!
 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Michael Tosc » Sun, 08 Jan 2006 01:29:18





>>>I am using printf through awk to pad with spaces a variable's data to
>>>the deired length, for example:

>>>echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

>>>This works great, and pads data up to 20 spaces if it isn't already.
>>>However, if $variable contains data such as "hello world", i.e with a
>>>space already with the data between the two words, the result is "hello
>>>             " rather than "hello world         ".

>>>Any ideas how to get around this?

>>Yes:

>>  echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

>>Awk splits its input line into separate fields, with the
>>separator being spaces and/or tabs.  So in your example
>>input $1 became "hello" and $2 became "world", but you're
>>only telling awk to give the first field ($1) to printf.

>>You seem to want awk to give the entire line to printf, so
>>you want to specify $0.

> Greg, top man, thanks very much!

I hope this was an example.
Otherwise better use printf without awk:

printf '%-20s\n' "$variable"

--

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Chris F.A. Johnso » Sun, 08 Jan 2006 01:28:52



> I am using printf through awk to pad with spaces a variable's data to
> the deired length, for example:

> echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

> This works great, and pads data up to 20 spaces if it isn't already.
> However, if $variable contains data such as "hello world", i.e with a
> space already with the data between the two words, the result is "hello
>               " rather than "hello world         ".

> Any ideas how to get around this?

   Why use awk? All POSIX systems have printf, and  bash, ksh93 and
   ash have it built in:

printf "%-20s\n" "$variable"

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Ed Morto » Sun, 08 Jan 2006 01:31:55


 >

 >>
 >>

 >>>
 >>>> I am using printf through awk to pad with spaces a variable's data to
 >>>> the deired length, for example:
 >>>>
 >>>> echo "${variable}" | awk '{ printf("%-20s\n",$1) }'
 >>>>
 >>>> This works great, and pads data up to 20 spaces if it isn't already.
 >>>> However, if $variable contains data such as "hello world", i.e with a
 >>>> space already with the data between the two words, the result is
"hello
 >>>>             " rather than "hello world         ".
 >>>>
 >>>> Any ideas how to get around this?
 >>>>
 >>>
 >>> Yes:
 >>>
 >>>  echo "${variable}" | awk '{ printf("%-20s\n",$0) }'
 >>>
 >>>
 >>> Awk splits its input line into separate fields, with the
 >>> separator being spaces and/or tabs.  So in your example
 >>> input $1 became "hello" and $2 became "world", but you're
 >>> only telling awk to give the first field ($1) to printf.
 >>>
 >>> You seem to want awk to give the entire line to printf, so
 >>> you want to specify $0.
 >>
 >>
 >>
 >> Greg, top man, thanks very much!
 >>
 >
 > I hope this was an example.
 > Otherwise better use printf without awk:
 >
 > printf '%-20s\n' "$variable"
 >

Or, if your shell doesn't support "printf", use awk without echo:

        awk 'BEGIN{printf("%-20s\n",ARGV[1]);exit}' "$variable"

Regards,

        Ed.

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Dundonal » Sat, 07 Jan 2006 23:19:27



> I am using printf through awk to pad with spaces a variable's data to
> the deired length, for example:

> echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

> This works great, and pads data up to 20 spaces if it isn't already.
> However, if $variable contains data such as "hello world", i.e with a
> space already with the data between the two words, the result is "hello
>               " rather than "hello world         ".

> Any ideas how to get around this?

I just had a thought, one workaround is to do a sed replace of spaces
with an arbitrary character such as *, perform the pad, then do a
replace of * with space again.  But that's two 'wasted' extra processes
that may not be needed, I'm sure there must be a way around it?
 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Dundonal » Sat, 07 Jan 2006 23:50:56



> I am using printf through awk to pad with spaces a variable's data to
> the deired length, for example:

> echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

> This works great, and pads data up to 20 spaces if it isn't already.
> However, if $variable contains data such as "hello world", i.e with a
> space already with the data between the two words, the result is "hello
>               " rather than "hello world         ".

> Any ideas how to get around this?

Just had an idea of a workaround where I could use a sed replace to
replace all occurrences of a space with an arbitrary character such as
*, perform the pad, then sed replace * with space.  But this is two
addition 'wasted' processes, I'm sure there must be a better solution?
 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Chris F.A. Johnso » Mon, 09 Jan 2006 02:04:21




>> I am using printf through awk to pad with spaces a variable's data to
>> the deired length, for example:

>> echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

>> This works great, and pads data up to 20 spaces if it isn't already.
>> However, if $variable contains data such as "hello world", i.e with a
>> space already with the data between the two words, the result is "hello
>>               " rather than "hello world         ".

>> Any ideas how to get around this?

> Just had an idea of a workaround where I could use a sed replace to
> replace all occurrences of a space with an arbitrary character such as
> *, perform the pad, then sed replace * with space.  But this is two
> addition 'wasted' processes, I'm sure there must be a better solution?

   What one earth are you trying to do?

   If you insist on using awk:

echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

   Otherwise use the shell:

printf "%-20s\n" "$variable"

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Ed Morto » Mon, 09 Jan 2006 03:21:41





>>>I am using printf through awk to pad with spaces a variable's data to
>>>the deired length, for example:

>>>echo "${variable}" | awk '{ printf("%-20s\n",$1) }'

>>>This works great, and pads data up to 20 spaces if it isn't already.
>>>However, if $variable contains data such as "hello world", i.e with a
>>>space already with the data between the two words, the result is "hello
>>>              " rather than "hello world         ".

>>>Any ideas how to get around this?

>>Just had an idea of a workaround where I could use a sed replace to
>>replace all occurrences of a space with an arbitrary character such as
>>*, perform the pad, then sed replace * with space.  But this is two
>>addition 'wasted' processes, I'm sure there must be a better solution?

>    What one earth are you trying to do?

>    If you insist on using awk:

> echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

awk 'BEGIN{printf("%-20s\n",ARGV[1]);exit}' "$variable"

- Show quoted text -

Quote:>    Otherwise use the shell:

> printf "%-20s\n" "$variable"

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Bill Marcu » Tue, 10 Jan 2006 08:41:13


On Sat, 07 Jan 2006 12:21:41 -0600, Ed Morton

>>    If you insist on using awk:

>> echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

> awk 'BEGIN{printf("%-20s\n",ARGV[1]);exit}' "$variable"

In the first command, awk operates on the contents of $variable, in the
second, it tries to open a file whose name is in $variable.

--
Life is wasted on the living.
                -- The Restaurant at the Edge of the Universe.

 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Kenny McCorma » Tue, 10 Jan 2006 10:22:27




>On Sat, 07 Jan 2006 12:21:41 -0600, Ed Morton

>>>    If you insist on using awk:

>>> echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

>> awk 'BEGIN{printf("%-20s\n",ARGV[1]);exit}' "$variable"

>In the first command, awk operates on the contents of $variable, in the
>second, it tries to open a file whose name is in $variable.

Think again.
 
 
 

printf to pad with spaces BUT trips over when data being padded contains spaces

Post by Bill Marcu » Tue, 10 Jan 2006 13:02:21


On Mon, 09 Jan 2006 01:22:27 GMT, Kenny McCormack



>>On Sat, 07 Jan 2006 12:21:41 -0600, Ed Morton

>>>>    If you insist on using awk:

>>>> echo "${variable}" | awk '{ printf("%-20s\n",$0) }'

>>> awk 'BEGIN{printf("%-20s\n",ARGV[1]);exit}' "$variable"

>>In the first command, awk operates on the contents of $variable, in the
>>second, it tries to open a file whose name is in $variable.

> Think again.

Oops, didn't notice the ARGV[1].

--
It's amazing how much better you feel once you've given up hope.

 
 
 

1. printf with left space padding & decimals

I want to get a column in percentages left space padded up to 3
positions ( for 100.00%) with 2 decimals, the symbol "%", 2 tabs and a
colum of right justified integers. The closest I got was:

10.96%             2
8.32%              3
3.33%              4
3.88%              5
3.61%              6
2.77%              7
2.22%              8
1.25%              9
2.77%             10
0.69%             11

That was with:

while read frequence quantite ;
do awk 'BEGIN { printf "%.2f%%\t\t %3d\n", ('$frequence' / '$nombre') *
100, '$quantite' }'  >> stat1.txt
done < stat.txt

What I really want is:

 10.96%            2
  8.32%            3
  3.88%            5
  3.61%            6
  2.77%            7
  2.22%            8
  1.25%            9
  2.77%           10
  0.69%           11

How can I get it?

2. anyone have XF86Config for NEC Versa S?

3. Padding Tabs with Spaces in 'vi'

4. V1280 12way CPU upgrade from 900Mhz to 1.2Ghz

5. Padding out a plain text file with a number of spaces?

6. Solaris 8 - convention for starting BIND 9 at boot

7. Padding file with spaces

8. how to make tgz package in Slackware?

9. exchange data from user space to kernel space

10. gcc pads structures that contain pointers

11. Query on code space and data space !

12. Writing Data from Kernel Space to User Space.

13. Sorting numeric data without padding