Cut in CLI doesn't work as expected - returns full lines every time
I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.
$ ls -l /usr | cut -f3
total 0
drwxr-xr-x 971 root wheel 31072 23 sty 20:05 bin
drwxr-xr-x 304 root wheel 9728 23 sty 20:05 lib
drwxr-xr-x 248 root wheel 7936 23 sty 20:05 libexec
drwxr-xr-x 16 root wheel 512 3 lis 10:50 local
drwxr-xr-x 239 root wheel 7648 23 sty 20:05 sbin
drwxr-xr-x 46 root wheel 1472 3 lis 10:41 share
drwxr-xr-x 5 root wheel 160 21 wrz 06:06 standalone
Specifying delimiters seems to work but not for the TAB character (which should be default).
$ ls -l /usr | cut -f3 -d' '
971
304
248
239
I'm using ZSH with Oh my zsh and iTerm 2 if it matters.
Solution 1:
You can squeeze the white spaces into a single white space in ls
's output then use cut
.
ls -l /usr | tr -s ' ' | cut -d ' ' -f3
but avoid parsing ls
output. Here's an alternate solution.
stat -f'%Su' /usr/*
Solution 2:
ls
doesn't use tabs, cut
doesn't work with a variable number of delimeters between fields.
ls -l /usr | awk '{print $3}'
will work, or
ls -l /usr | awk 'NR > 1 {print $3}'
if you want to skip the first line (total 0
in your example).