Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #181142 > unrolled thread

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

Started byrhkramer@gmail.com
First post2017-05-20 15:20 +0200
Last post2017-05-20 21:30 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.debian.user


Contents

  OT: Help with  sort (and maybe awk or sed) rhkramer@gmail.com - 2017-05-20 15:20 +0200
    Re: OT: Help with  sort (and maybe awk or sed) Stefan Monnier <monnier@iro.umontreal.ca> - 2017-05-20 15:30 +0200
      Re: OT: Help with  sort (and maybe awk or sed) Andre Majorel <aym-naibed@teaser.fr> - 2017-05-20 21:10 +0200
      Re: OT: Help with  sort (and maybe awk or sed) rhkramer@gmail.com - 2017-05-20 21:30 +0200

#181142 — OT: Help with sort (and maybe awk or sed)

Fromrhkramer@gmail.com
Date2017-05-20 15:20 +0200
SubjectOT: Help with sort (and maybe awk or sed)
Message-ID<tJcsy-1Xo-35@gated-at.bofh.it>
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.  

My first thought (well, actually it's probably the sixth, one of which was 
trying to get the TWiki developers to reverse the order of the fields, i.e. 
[[<Title1>][<URL1>]]) is to sort on the "field" starting with "]".

I don't see a way to set such a delimiter for a field in man sort, so I think I 
need something like awk or sed (or some other program) in addition to sort.

My sed or awk skills aren't up to doing something like that, so any help would 
be appreciated.

I will use the sort in a variety of  (that can invoke command line commands) 
which include nedit, kate, and any of a number of Scintilla based editor.  (It 
does not include vi, vim, or emacs.)

Thanks!
Randy Kramer

[toc] | [next] | [standalone]


#181143

FromStefan Monnier <monnier@iro.umontreal.ca>
Date2017-05-20 15:30 +0200
Message-ID<tJcCe-20L-9@gated-at.bofh.it>
In reply to#181142
> 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,


        Stefan

[toc] | [prev] | [next] | [standalone]


#181157

FromAndre Majorel <aym-naibed@teaser.fr>
Date2017-05-20 21:10 +0200
Message-ID<tJhVg-5rT-21@gated-at.bofh.it>
In reply to#181143
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.

[toc] | [prev] | [next] | [standalone]


#181158

Fromrhkramer@gmail.com
Date2017-05-20 21:30 +0200
Message-ID<tJieB-5A2-5@gated-at.bofh.it>
In reply to#181143
On Saturday, May 20, 2017 09:19:07 AM 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,

Thanks!  Two wows!

   1. Wow, thanks (again)!

   2.  Wow, how did I miss the -t option when I read (well, I suppose, 
skimmed) man sort this morning.  I must have been asleep!

regards,
Randy Kramer

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web