Re: cut bug?

Andrew D Jewell <[email protected]> Thu, 7 Nov 2002 13:56:38 -0500
Newsgroups gmane.comp.gnu.textutils.bugs
Message-ID <a0511171ab9f06734a1e4@[209.247.255.161]>
Alternately, you can used the enhanced cut in

coreutils-4.5.3-alexa02.tar.gz

at

http://alexautils.sourceforge.net/

cut, as well as all the other field based tools, supports --dw to set 
the delimiter to "whitespace" and does what you want.

adj



At 11:16 AM -0700 11/7/02, Bob Proulx wrote:
>Bill Cecchin <[email protected]> [2002-11-01 10:27:37 -0800]:
>>  I am not sure if this is a bug or just lack of knowledge on my part.  In
>>  Unix I could always use 'cut' to pull the needed fields out of our text
>>  files.  In GNU Utilities I can't find any combination of parameters that
>>  will allow using 'cut' on a file delimited by spaces. Is it possible to
>>  cut a file using a SPACE as a delimiter between fields?
>
>The cut program is a very old program and on all unixlike systems of
>which I am aware the default field delimiter is a TAB.  The original
>use of the program was for tabular data in data files which had fields
>separated by TABs.
>
>You can set the field delimiter to be anything you want using the -d
>option.  That is a standard option and should work on all unixlike
>systems.
>
>   cut -d ' ' -f 2
>
>But the behavior is such that this is probably not what you want to
>do.  The cut program will treat a leading space as an empty field by
>definition.  The field numbers are different if there are leading
>spaces or not.  This is not a bug but part of the definition and use
>of the cut command.  Therefore awk is really a better choice for most
>free form data that you just want to pull out fields.
>
>Using cut you can see that it is sensitive to field separators.
>Although the use it is put to will define this.  It all depends on
>what you want.
>
>   echo one two three | cut -d ' ' -f 2
>   two
>
>   echo "one two three" | cut -d ' ' -f 2
>   two
>
>   echo " one two three" | cut -d ' ' -f 2
>   one
>
>Using awk most people find this result more intuitive.
>
>   echo one two three | awk '{print $2}'
>   two
>
>   echo "one two three" | awk '{print $2}'
>   two
>
>   echo " one two three" | awk '{print $2}'
>   two
>
>Bob
>
>
>_______________________________________________
>Bug-textutils mailing list
>[email protected]
>http://mail.gnu.org/mailman/listinfo/bug-textutils