質問

When I do ls -l I get

-rw-r--r--   1 jboss    admin  **26644936** Sep  1 21:23 MyBig.war

How do I print it as below

-rw-r--r--   1 jboss    admin  **26,644,936** Sep  1 21:23 MyBig.war
役に立ちましたか?

解決

The proper way to format ls output is to specify BLOCK_SIZE.

Saying:

BLOCK_SIZE="'1" ls -l

would achieve your desired result.

Quoting from the above link:

Some GNU programs (at least df, du, and ls) display sizes in “blocks”. You can adjust the block size and method of display to make sizes easier to read.


A block size specification preceded by ‘'’ causes output sizes to be displayed with thousands separators.

他のヒント

Using sed:

$ ls_output='-rw-r--r--   1 jboss    admin  26644936 Sep  1 21:23 MyBig.war'
$ echo $ls_output | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
-rw-r--r--   1 jboss    admin  26,644,936 Sep  1 21:23 MyBig.war

Above sed command repeatedly replace the last 4 digits #### with #,###.

  • -e :a: Make a label named a for t command.
  • ta: Jump to a if substitution was successful.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top