bug-coreutils
[Top][All Lists]
Advanced

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

Feature request: handling space-delimited data with cut


From: Andrew McGill
Subject: Feature request: handling space-delimited data with cut
Date: Fri, 15 May 2009 12:10:17 +0200
User-agent: KMail/1.9.10

A novice will cut out a list of PIDs like this:

 ps uaxw | grep nobody | cut -f 2          # huh?
 ps uaxw | grep nobody | cut -f 2 -d ' '   # oh, not tabs, but spaces. huh?
 ps uaxw | grep nobody | cut -f 4 -d ' '   # 'nobody' is followed by 3 spaces

After consulting 3 friends and 6 linux mailing lists about that one random 
line it prints out, they learn that the canonical ways of doing this simple 
task are all just horrid:

 ps uaxw | grep nobody | tr -s ' ' | cut -f 4 -d ' ' 
 ps uaxw | grep nobody | awk '{print $2}'
 ps uaxw | grep nobody | while read x y z; do echo $y; done


There are two problems:

 * Input is not tab delimited.  It might once have been, but currently the
   most interesting input is space delimited.

 * There is no SIMPLE tool for handling space delimited data ({'$awk'} is not
   simple, IMHO)


There are a few ways of fixing this:

 1. Adding an option (-w) similar to -f to cut words separated by whitespace 
    (same rules as sort, unless the whitespace is changed to something else
    with -d)

        ps uaxwf | grep nobody | cut -w 2
        ps uaxwf | grep nobody | cut --words 2

 2. Adding a switch (-w) similar to set the delimiter to multiple whitespace

        ps uaxwf | grep nobody | cut -w -f 2
        ps uaxwf | grep nobody | cut --whitespace -f 2

 3. Adding an option (-m) to merge delimiters, similar to what tr -s does, but 
    without having to specify the delimiter twice on the command line

        ps uaxwf | grep nobody | cut -m -d ' ' -f 2
        ps uaxwf | grep nobody | cut --merge-delimiters -d ' ' -f 2

 4. Adding out of band meta information about the input stream, thereby  
    violating everything unixy, and condemning generations of children not to 
    understand that the scissors they are running with are sharp:

        ps uaxwf | grep nobody | cut -f 2

    It could be called "Alternate Data Streams" and "Extended Attributes"

 5. Embark on a campaign of education and training so that the true path to 
    awk and tr enlightenment will be known to all.

I'm in favour of cut -w ... can I send a patch?

&:-)




reply via email to

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