How to get nth column with regexp delimiter [duplicate]
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 '[^ ]+$'
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).
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.