8

Basically I get line from ls -la command:

-rw-r--r--  13 ondrejodchazel  staff  442 Dec 10 16:23 some_file

and want to get size of file (442). I have tried cut and sed commands, but was unsuccesfull. Using just basic UNIX tools (cut, sed, awk...), how can i get specific column from stdin, where delimiter is / +/ regexp?

weberc2
  • 7,423
  • 4
  • 41
  • 57
Ondra
  • 3,100
  • 5
  • 37
  • 44

3 Answers3

18

If you want to do it with cut you need to squeeze the space first (tr -s ' ') because cut doesn't support +. This should work:

ls -la | tr -s ' ' | cut -d' ' -f 5

It's a bit more work when doing it with sed (GNU sed):

ls -la | sed -r 's/([^ ]+ +){4}([^ ]+).*/\2/'

Slightly more finger punching if you use the grep alternative (GNU grep):

ls -la | grep -Eo '[^ ]+( +[^ ]+){4}' | grep -Eo '[^ ]+$'
Thor
  • 45,082
  • 11
  • 119
  • 130
  • +1 for 'sed | cut' flow. By far the most flexible, powerful, concise technique for column grokking. – gepoch Jan 23 '14 at 17:56
5

Parsing ls output is harder than you think. Use a dedicated tool such as stat instead.

size=$(stat -c '%s' some_file)

One way ls -la some_file | awk '{print $5}' could break is if numbers use space as a thousands separator (this is common in some European locales).

See also Why You Shouldn't Parse the Output of ls(1).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Thanks, your answer is good, but parsing ls output was rather example of usage, then the real usage. – Ondra Dec 11 '12 at 14:37
2

Pipe your output with:

awk '{print $5}'

Or even better us to use stat command like this (On Mac):

stat -f "%z" yourFile

Or (on Linux)

stat -c "%s" yourFile

that will output size of file in bytes.

anubhava
  • 761,203
  • 64
  • 569
  • 643