Unix Shell - ls display

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > ls display





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ls display
Gary Wessle

2007-02-17, 1:17 pm

hi

is there a way to display the file size in
1,345,334,443
notation instead of
1345334443
?

thanks
Bit Twister

2007-02-17, 1:17 pm

On 18 Feb 2007 04:44:48 +1100, Gary Wessle wrote:
> hi
>
> is there a way to display the file size in
> 1,345,334,443
> notation instead of
> 1345334443
> ?


You could write a script or just use the -h switch with ls.
Stephan Grein

2007-02-17, 1:17 pm

Gary Wessle wrote:
> hi
>
> is there a way to display the file size in
> 1,345,334,443
> notation instead of
> 1345334443
> ?
>
> thanks


echo "1345334443" | PERL -ne '$_=reverse; s/(\d{3})/$1,/g; print scalar
reverse;'

How about that?

HTH,
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Chris F.A. Johnson

2007-02-17, 7:15 pm

On 2007-02-17, Bit Twister wrote:
> On 18 Feb 2007 04:44:48 +1100, Gary Wessle wrote:

What program are you using to generate the file sizes?
[vbcol=seagreen]
> You could write a script or just use the -h switch with ls.


The -h option is non-standard, and on those implementation which do
have, do not give that format.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane CHAZELAS

2007-02-18, 7:17 pm

2007-02-18, 04:44(+11), Gary Wessle:
> hi
>
> is there a way to display the file size in
> 1,345,334,443
> notation instead of
> 1345334443
> ?

[...]

number=1345334443
ts=$(locale thousands_sep) &&
printf '%s\n' "$number" | sed ":1
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1$ts\2/;t1"

--
Stéphane
Stephane CHAZELAS

2007-02-18, 7:17 pm

2007-02-17, 19:17(+01), Stephan Grein:
> Gary Wessle wrote:
>
> echo "1345334443" | PERL -ne '$_=reverse; s/(\d{3})/$1,/g; print scalar
> reverse;'
>
> How about that?

[...]

$ echo "345334443" | PERL -ne '$_=reverse; s/(\d{3})/$1,/g;
> print scalar reverse;'

,345,334,443

--
Stéphane
Phil Jackson

2007-02-18, 7:17 pm

All,

Stephane CHAZELAS <this.address@is.invalid> writes:

> 2007-02-17, 19:17(+01), Stephan Grein:
> [...]
>
> $ echo "345334443" | PERL -ne '$_=reverse; s/(\d{3})/$1,/g; print
> scalar reverse;' ,345,334,443


'$_=reverse; s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; print scalar reverse;'
345,334,443

Cheers,
Phil
--
Oh, so they have internet on computers now!
Xicheng Jia

2007-02-18, 7:17 pm

On Feb 18, 2:54 pm, Phil Jackson <s...@place-spam-here.blah> wrote:
> All,
>
> Stephane CHAZELAS <this.addr...@is.invalid> writes:
>
>
>
>
> '$_=reverse; s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; print scalar reverse;'
> 345,334,443
>


For intergers:

echo "345334443" | PERL -pe 's/(?<=\d)(?=(?:\d\d\d)+\b)/,/g'

Regards,
Xicheng

Chris F.A. Johnson

2007-02-18, 7:17 pm

On 2007-02-18, Xicheng Jia wrote:
> On Feb 18, 2:54 pm, Phil Jackson <s...@place-spam-here.blah> wrote:
>
> For intergers:
>
> echo "345334443" | PERL -pe 's/(?<=\d)(?=(?:\d\d\d)+\b)/,/g'


For integers and decimal fractions, and many times faster than perl
or other external command:

_commas() {
_COMMAS=
_DECPOINT=. ## Adjust for other locales
_TH_SEP=, ## ditto
case $1 in
"$_DECPOINT"*) _COMMAS=$1 ## Number begins with dot; no action needed
return
;;
*"$_DECPOINT") ## Number ends with dot; store it in $c_decimal
c_num=${1%"$_DECPOINT"}
c_decimal=.
;;
*"$_DECPOINT"*) ## Number contains a dot; split into integer and decimal
c_num=${1%"$_DECPOINT"*} ## Separate integer and fraction
c_decimal=$_DECPOINT${1#*"$_DECPOINT"}
;;
*) c_num=$1 ## No dot, therefore no decimal
c_decimal=
;;
esac

while :
do
case $c_num in
## Three or fewer digits [left] in $num;
## add them to the front of _COMMAS and exit from loop
???|??|?) _COMMAS=${c_num}${_COMMAS:+"$_TH_SEP"$_COMMAS}
break
;;
*) ## More than three numbers in $num
left=${c_num%???} ## All but the last three digits

## Prepend the last three digits and a comma
_COMMAS=${c_num#${left}}${_COMMAS:+"$_TH_SEP"$_COMMAS}
c_num=$left ## Remove last three digits
;;
esac
done

## Replace decimal fraction, if any
_COMMAS=${_COMMAS}${c_decimal}
}

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane CHAZELAS

2007-02-18, 7:17 pm

2007-02-18, 15:38(-05), Chris F.A. Johnson:
[...]
> For integers and decimal fractions, and many times faster than perl
> or other external command:


YMMV, to process 100000 numbers, here, it took 3 minutes with
your function for pdksh, 22 seconds for bash, 14 seconds for
ksh93, 11 seconds for zsh.

It took 1.2 seconds to

sed ':1
s/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t1'

And 0.9 seconds to

perl -pe '$_=reverse;s/\d{3}(?=\d)(?!.*?\.)/$&,/g;$_=reverse'

>
> _commas() {

[snipped long and illegible function]
> }
>



--
Stéphane
Chris F.A. Johnson

2007-02-19, 1:21 am

On 2007-02-18, Stephane CHAZELAS wrote:
> 2007-02-18, 15:38(-05), Chris F.A. Johnson:
> [...]
>
> YMMV, to process 100000 numbers, here, it took 3 minutes with
> your function for pdksh, 22 seconds for bash, 14 seconds for
> ksh93, 11 seconds for zsh.


But you know better than to use a shell loop (which is what you are
measuring, not the time it takes the function) for something like
that, just as I know better than to use an external command for
something that can be done entirely (and efficiently) in the shell.

> It took 1.2 seconds to
>
> sed ':1
> s/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t1'
>
> And 0.9 seconds to
>
> PERL -pe '$_=reverse;s/\d{3}(?=\d)(?!.*?\.)/$&,/g;$_=reverse'
>
> [snipped long and illegible function]


You must have been reading a different function. There was nothing
illegible (or unintelligble) for anyone with a modicum of knowledge
of the POSIX shell (let alone you) in the function I posted.
[vbcol=seagreen]


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephan Grein

2007-02-19, 7:20 am

Stephane CHAZELAS wrote:
> 2007-02-17, 19:17(+01), Stephan Grein:
> [...]
>
> $ echo "345334443" | PERL -ne '$_=reverse; s/(\d{3})/$1,/g;
> ,345,334,443
>

Sorry, I just tested this with one number and it worked, but the
lookarounds seem quite working nice now.

--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
slakmagik

2007-02-20, 1:17 pm

On 2007-02-17 Sat 12:44:48, Gary Wessle wrote:
> hi
>
> is there a way to display the file size in
> 1,345,334,443
> notation instead of
> 1345334443
> ?
>
> thanks


Sorry, this is a bit late (catching up on my reading) and you seem to
have several answers already. And, as another solution was, this is
completely non-standard, but if you just want it for your personal use
and have a relatively recent FSF coreutils ls and
LANG/LC_ALL/LC_NUMERIC/whatever set to something useful for your
purpose, and I understand the question correctly, then another option
for showing the thousands separators is:

:ls -l /usr/bin/gs
-rwxr-xr-x 1 root root 4038760 2006-09-18 04:39 /usr/bin/gs

:ls -l --block-size="'1" /usr/bin/gs
-rwxr-xr-x 1 root root 4,038,760 2006-09-18 04:39 /usr/bin/gs

(The apostrophe and the locale are the keys.)

And since that's a horrible thing to have to type, you could

alias lsc='ls --block-size="'''1"'

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com