coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Add option to `cut` to avoid empty fields when using -d


From: Bob Proulx
Subject: Re: Add option to `cut` to avoid empty fields when using -d
Date: Sat, 17 Oct 2015 23:47:23 -0600
User-agent: Mutt/1.5.24 (2015-08-30)

Shriramana Sharma wrote:
> Hello. I often try to use `cut` to get specific columns from `ls -l`
> but since `ls` uses spaces as padding for aligning fields, I am not
> able to do something like this to get the sizes alone (just for
> illustration):

There are two points of confusion.  First is that cut is not the best
tool for parsing whitespace delimited files.

> $ ls -l
> total 1632
> -rwxrwxr-x 1 samjnaa samjnaa  548912 Oct 18 08:20 add-commas
> -rw-rw-r-- 1 samjnaa samjnaa     752 Oct 17 15:32 add-commas.d
> -rw-rw-r-- 1 samjnaa samjnaa 1109848 Oct 18 08:20 add-commas.o
> -rw-rw-r-- 1 samjnaa samjnaa     413 Oct 18 08:19 compile
> $ ls -l | cut -d ' ' -f5
> 1109848

The best tool for this is awk.  If you really want to parse 'ls -l'
fields then awk is the best way to do it.  The utilities cut, paste,
join were designed to work together on tab separated tables.  Not the
best tools for whitespace delimited data files.  For whitespace
delimited files awk is best.

  $ ls -l | awk '{print$5}'
  1109848

> I request that an option may be added to collapse multiple consecutive
> instances of the same delimiter to avoid empty fields in the output in
> use cases such as above.

The reason that cut doesn't grow these types of creeping features that
cause code bloat is because it is completely unnecessary due to the
feature already existing in awk.  Plus awk is a standardized tool
which must exist the same everywhere.  Standard is better since it is
portable everywhere.  That is much better than a non-standard option
that only exists some places.  The above will work on any GNU-like or
Unix-like system.

Second is that 'ls -l' is not the best tool for extracting file
information.  If avaliable then better is to use a tool such as 'stat'
for extracting this information.  Unfortunately it isn't one of the
standardized tools.  It is available on many systems however.  It does
precisely solve the needed task best.

  $ stat --format %s add-commas.o
  1109848

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]