I tried to use cut command to get a list of file names and their sizes from "ls -l" command output.
$ ls -l | cut -f 5,9 -d " "
It gives me output based on 'SINGLE WHITE SPACE' as a delimiter. When "ls -l" output contains consecutive spaces in certain rows, then the output of the command is not proper for those rows.
The rows which have only single white space as column separator, give correct output.
When I run following command:
$ ls -l | awk '{ print $5"\t"$9 }'
awk is ignoring multiple spaces and properly extracting columns from "ls -l" output.
While, cut is treating each space as a delimiter, there by putting values in wrong columns.
It gives correct output for all rows.
Why is this happening ? What can I do to work this out with cut command ?
Best Answer
awk splits fields on whitespace. cut splits fields on a delimiting character. awk is the better tool for this problem.
As an alternative, you can pipe
ls -l
into a utility that either compresses multiple space chars (maybetr -s
), or into a utility that replaces multiple space chars with a single one (maybesed
). Then cut will do what you want it to.