Re: tab as sort's field-separator

[email protected] (Bob Proulx)
Newsgroups gmane.comp.gnu.textutils.bugs
Message-ID <[email protected]>
> Let's try what the manual suggests:
> 
>   sort -t$'\t'. -u -k2,2 -k1,1 /dev/null
> 
> That looks good so far and works with bash, hpux /bin/sh, and aix
> /bin/sh which I tested this out on.  If three different sources do
> something the same way then there must be a reason.
> 
> I perused the standards documentation for the shell here.
> 
>   http://www.opengroup.org/onlinepubs/007908799/xcu/chap2.html
> 
> But unfortunately I did not find anything that required this.  Perhaps
> I missed it and someone can point me to the relevant passages.

Then Paul kindly replied in another thread which I will join in here:

> > But I can't find anything in the standards documentation about how
> > to
> > quote tabs using $'\t' syntax.
>
> That's because the $'\t' syntax is not standardized by POSIX.

I was afraid you might say that.  Oh well.  I personally will be
avoiding that construct then.  Thanks for that information.  That was
exactly what I was looking for.

> Here's what I do in this situation:
>
>    tab='      ' # <-- This is a single tab character.
>    sort -t"$tab" -u -k2,2 -k1,1

I have done that in the past but someone will assuredly cut-n-paste
that and lose the tab by turning it into a collection of spaces.

> If you're worried that your shell script's tabs will be expanded,
> you
> can do this instead:
>
>    tab=`awk 'BEGIN {print "\t"; exit}'`
>    sort -t"$tab" -u -k2,2 -k1,1

At least that is very portable and survives the cut-n-paste test.  And
it is reliable whereas bash's echo -e option which was previously
mentioned is not.

But instead of that awk I like printf(1) here instead.  Given the
information you provided I would go with this.

   tab=$(printf "\t")
   sort -t"$tab" -u -k2,2 -k1,1

This appears to be POSIX standard.  Or I suppose you could combine
them into a one-liner.

   sort -t"$(printf "\t")" -u -k2,2 -k1,1

Bob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.