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.
