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

Re: OT: Help with sort (and maybe awk or sed)



On 2017-05-20 09:19 -0400, Stefan Monnier wrote:
> > I have a need to sort lists of URLs and associated titles formatted as 
> > follows:
> >
> >    * [[<URL1>][<Title1>]]
> >
> > e.g [[http://www.google.com][Google search]]
> >
> > I'd like to get a simple sort routine to do that.  
> 
> In my quick test,
> 
>     sort -t '[' -k 4
> 
> seemed to do the trick,

Another approach is to convert the list to something more
"normal", say two fields separated by a tab, process it with the
usual tools and convert it back.

perl -n -e 'BEGIN { $l = "\\["; $r = "\\]"; $nonr = "[^[]" }
  /^$l$l($nonr*)$r$l($nonr*)$r$r$/ or die "malformed line";
  die "URL contains a tab" if $1 =~ /\t/;
  die "description contains a tab" if $2 =~ /\t/;
  print "$1\t$2\n";'

This script also catches malformed or ambiguous input but relies
on no tabs in the input.

If you trust the input to be correct and free of corner cases
like embedded "]" and all you want is to sort it, Stefan's
approach is fine, I would think.

-- 
André Majorel <http://www.teaser.fr/~amajorel/>
Ever got spam through an address harvested from lists.debian.org ?
Neither have I.


Reply to: