bug-coreutils
[Top][All Lists]
Advanced

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

bug#20928: cut (v. 8.21), using -f option with a single column


From: Pádraig Brady
Subject: bug#20928: cut (v. 8.21), using -f option with a single column
Date: Mon, 29 Jun 2015 17:25:11 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

On 29/06/15 16:54, Stephane Chazelas wrote:
> 2015-06-29 16:31:00 +0100, Pádraig Brady:
> [...]
>>> When there is only one column and we go beyond 1 with the -f option, the 
>>> output remains the first column
>>>
>>> $ echo "test1" | cut -d' ' -f1
>>> test1
>>> $ echo "test1" | cut -d' ' -f2
>>> test1
>>> $ echo "test1" | cut -d' ' -f3
>>> test1
>>
>> That difference in behavior is there for compat reasons.
> 
> Yes, and that's required by POSIX
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cut.html
> 
>> To induce the behavior you expect, you need the -s option.
> [...]
> 
> -s suppresses the lines that don't have the field which is
> different from outputting a blank field. Here, the OP more
> likely wants:
> 
> paste -d ' ' - /dev/null | cut -d' ' -f3
> (or awk -F'[ ]' '{print $3}')
> 
> $ printf '%s\n' a:b c d:e | cut -d: -f2
> b
> c
> e
> $ printf '%s\n' a:b c d:e | cut -sd: -f2
> b
> e
> $ printf '%s\n' a:b c d:e | paste -d: - /dev/null | cut -d: -f2
> b
> 
> e

Good point. Or to better support field ranges:

$ printf '%s\n' a:b c d:e | sed 's/^[^:]*$/&:/' | cut -d: -f2-
b

e

cheers,
Pádraig.





reply via email to

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