简体   繁体   中英

Cut and Awk command : Delimiter behaviour

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 ?

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 (maybe tr -s ), or into a utility that replaces multiple space chars with a single one (maybe sed ). Then cut will do what you want it to.

Don't parse ls -- your code will not print the full filename if it contains spaces. To get the file size and name, use stat :

stat -c "%s %n" *

尝试这个?:

ls -l | tr -s ' ' | cut -d ' ' -f 5, 9 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM