Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tr squeeze repeats does not remove multiple spaces coming from a piped du command

4 views
Skip to first unread message

Zangune

unread,
Oct 3, 2018, 11:06:23 AM10/3/18
to
Greetings, I want to know the total size of a directory content, so I
use this command:

du -c /path | tr -s ' '

but I got this (this is just the last line):

510 total

please note that there are 4 spaces between "0" and "t".
I would expect the result to be:

510 total

please note that there is just one space between "0" and "t".
As far as I can see, the "du" command result is alined in columns with
spaces, but the "tr" command does not remove those repeated spaces like
it does in

echo 'a b' | tr -s ' '

Why doesn't a "tr" command with the "squeeze repeats" option remove
repeated spaces of a piped "du" command result?
Please note that I found a workaround, this one:

echo $(du -c /path)

but I am more interested in the theoretical problem solution (and
logic), rather than the practical one.
Regards.

Michael F. Stemper

unread,
Oct 3, 2018, 1:47:34 PM10/3/18
to
On 2018-10-03 10:05, Zangune wrote:
> Greetings, I want to know the total size of a directory content, so I
> use this command:
>
> du -c /path | tr -s ' '
>
> but I got this (this is just the last line):
>
> 510    total
>
> please note that there are 4 spaces between "0" and "t".

Are you quite sure? I just tried the same thing, and didn't find any
spaces between the number and the word "total". What I did find was
a tab:

username@hostname$ du -c dtd
84 dtd
84 total
username@hostname$ du -c dtd | sed 's/ /s/g'
84 dtd
84 total
username@hostname$ du -c dtd | sed 's/\t/tab/g'
84tabdtd
84tabtotal
username@hostname$

> I would expect the result to be:
>
> 510 total
>
> please note that there is just one space between "0" and "t".
> As far as I can see, the "du" command result is alined in columns with
> spaces,

Nope, tabs are much better for column alignment than are spaces. They
yield, one might say, a "tabular" format.

--
Michael F. Stemper
Psalm 94:3-6

Grant Taylor

unread,
Oct 3, 2018, 1:48:21 PM10/3/18
to
On 10/03/2018 09:05 AM, Zangune wrote:
> Why doesn't a "tr" command with the "squeeze repeats" option remove
> repeated spaces of a piped "du" command result?

My first guess is that you're telling du to look for a space, which
doesn't include tabs like I see in my du's output.

I tend to prefer awk when I'm wanting to do things in a white space
length agnostic way.



--
Grant. . . .
unix || die

Zangune

unread,
Oct 3, 2018, 7:51:00 PM10/3/18
to
"Michael F. Stemper" wrote

> du -c dtd | sed 's/\t/tab/g'
> 84tabdtd
> 84tabtotal

Thanks, I can confirm that a sed substitution shows me that there is a
single "tab" between the last number and the "total" word.
Indeed, if I leftclick on a terminal just after the last number and I
highlight what I can find between the numbers and the "total" word, the
highlight "trace" shows me four spaces; more than this, the highlighted
characters are automatically copied and, when pasted, they appear as
four spaces. That fooled me, but I guess this behaviour of the program I
use for terminal emulation is intentional, I have to investigate about
that.
Regards.

Zangune

unread,
Oct 3, 2018, 7:55:12 PM10/3/18
to
"Grant Taylor" wrote:

> you're telling du to look for a space, which doesn't include tabs like
> I see in my du's output

Thank you for the "tab" hint.
I believe you meant "tr" instead of "du" in this sentence, before the
comma.
It is a single "tab", but, as I wrote to "Michael F. Stemper" under his
first reply, I get fooled by my terminal emulator behaviour, my bad
anyway.
Regards.

Grant Taylor

unread,
Oct 4, 2018, 12:47:15 AM10/4/18
to
On 10/03/2018 05:54 PM, Zangune wrote:
> Thank you for the "tab" hint.

You're welcome.

> I believe you meant "tr" instead of "du" in this sentence, before the
> comma.

Oops. Thank you for helping me help you. ():-)

> It is a single "tab", but, as I wrote to "Michael F. Stemper" under his
> first reply, I get fooled by my terminal emulator behaviour, my bad anyway.
> Regards.

Ya. I've run into things like that before. I will occasionally use od,
xxd, or vim - to look at what comes from STDOUT.
0 new messages