[Top][All Lists]

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

Re: tab as sort's field-separator

From: Bob Proulx
Subject: Re: tab as sort's field-separator
Date: Mon, 17 Jun 2002 23:15:09 -0600
User-agent: Mutt/1.3.28i

> 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


reply via email to

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