Unix Shell - How to sort one file by another?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > How to sort one file by another?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to sort one file by another?
jose_luis_fdez_diaz

2006-12-14, 1:29 pm


Hi,

Given these two files:

m3vmsa3.caddebug /tmp > cat k1
one
two
three
m3vmsa3.caddebug /tmp > cat k2
three 3
two 2
one 1



I want to get this one:
one 1
two 2
three 3


Is there any unix tool to do it?

Thanks in advance,
Jose Luis.

Stephan Grein

2006-12-14, 1:29 pm

jose_luis_fdez_diaz wrote:
> Hi,
>
> Given these two files:
>
> m3vmsa3.caddebug /tmp > cat k1
> one
> two
> three
> m3vmsa3.caddebug /tmp > cat k2
> three 3
> two 2
> one 1
>
>
>
> I want to get this one:
> one 1
> two 2
> three 3
>
>
> Is there any unix tool to do it?
>
> Thanks in advance,
> Jose Luis.
>


awk '{ printf "%s %d\n",$0, ++COUNT }' k1

HTH,
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
OldSchool

2006-12-14, 1:29 pm


jose_luis_fdez_diaz wrote:
> Hi,
>
> Given these two files:
>
> m3vmsa3.caddebug /tmp > cat k1
> one
> two
> three
> m3vmsa3.caddebug /tmp > cat k2
> three 3
> two 2
> one 1
>
>
>
> I want to get this one:
> one 1
> two 2
> three 3
>
>
> Is there any unix tool to do it?
>
> Thanks in advance,
> Jose Luis.


So, if K1 was
one
three
two


you want to see
one 1
three 3
two 2
?

or did I mis-understand the desired results?

Janis Papanagnou

2006-12-14, 1:29 pm

jose_luis_fdez_diaz wrote:
> Hi,
>
> Given these two files:
>
> m3vmsa3.caddebug /tmp > cat k1
> one
> two
> three
> m3vmsa3.caddebug /tmp > cat k2
> three 3
> two 2
> one 1
>
>
>
> I want to get this one:
> one 1
> two 2
> three 3
>
>
> Is there any unix tool to do it?


With your subject line "How to sort one file by another?" I assume that
you want k1 be the file that defines the key order depending on the line
where the keys appear in the file and you want k2 to be sorted by the
identical key set in the first column according to the order in k1. Yes?

awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' k2 k1


Janis

>
> Thanks in advance,
> Jose Luis.
>

jose_luis_fdez_diaz

2006-12-15, 7:33 am


OldSchool wrote:
> jose_luis_fdez_diaz wrote:
>
> So, if K1 was
> one
> three
> two
>
>
> you want to see
> one 1
> three 3
> two 2
> ?


Yes.

>
> or did I mis-understand the desired results?


Regards,
Jose Luis

jose_luis_fdez_diaz

2006-12-15, 7:33 am


Janis Papanagnou wrote:[vbcol=seagreen]
> jose_luis_fdez_diaz wrote:
>
> With your subject line "How to sort one file by another?" I assume that
> you want k1 be the file that defines the key order depending on the line
> where the keys appear in the file and you want k2 to be sorted by the
> identical key set in the first column according to the order in k1. Yes?
>
> awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' k2 k1
>
>
> Janis
>

William James

2006-12-15, 7:33 am

OldSchool wrote:
> jose_luis_fdez_diaz wrote:
>
> So, if K1 was
> one
> three
> two
>
>
> you want to see
> one 1
> three 3
> two 2
> ?
>
> or did I mis-understand the desired results?


ruby -lne 'a||=[];a << [$_,$.].join(" ");END{puts a.sort}' myfile

Stephane CHAZELAS

2006-12-15, 7:33 am

2006-12-14, 08:39(-08), jose_luis_fdez_diaz:
>
> Hi,
>
> Given these two files:
>
> m3vmsa3.caddebug /tmp > cat k1
> one
> two
> three
> m3vmsa3.caddebug /tmp > cat k2
> three 3
> two 2
> one 1
>
>
>
> I want to get this one:
> one 1
> two 2
> three 3

[...]

You could to:

nl k1 |
sort -k2 |
join -j1 2 - <(sort k2) |
sort -k2n |
cut -d" " -f1,3-

<(...) is a non-standard feature of zsh, bash and some kshs.

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com