Subject: Re: shell script question (grep)
To: Lubos Vrbka <shnek@chemi.muni.cz>
From: Paul Chakravarti <paulc@passtheaardvark.com>
List: netbsd-help
Date: 02/09/2003 12:45:05
On Sunday, February 9, 2003, at 10:42 AM, Lubos Vrbka wrote:
> hi,
> sorry for a stupid (and slightly off-topic) question, but i cannot
> find a
> solution myself :o( i'm writing a shellscript for following purpose:
>
> i've 2 files: first file contains 7 columns, in the last one there's a
> number A. A isn't unique, it may occur on any number of lines. in the
> second
> file i've a "translation table", i.e. for every number A, there's
> given
> some other number B. there the A values are unique. now i want to
> replace
> all A values in the first file with the appropriate B values from the
> second
> file. i wanted to use following expression:
>
If the output order isn't important you can just use 'join'
% cat t1
AA BB CC DD EE FF 1
GG HH II JJ KK LL 2
MM NN OO PP QQ RR 3
SS TT UU VV WW XX 1
% cat t2
1 aaaa
3 cccc
2 bbbb
% sort +6n t1 > t1.sort
% sort +0n t2 > t2.sort
% join -1 7 -2 1 t1.sort t2.sort
1 AA BB CC DD EE FF aaaa
1 SS TT UU VV WW XX aaaa
2 GG HH II JJ KK LL bbbb
3 MM NN OO PP QQ RR cccc
Note that by default this outputs the join field followed by the
remaining fields from t1/t2 however you can modify this using the -o
option
% join -1 7 -2 1 -o '1.1 1.2 1.3 1.4 1.5 1.6 2.2' t1.sort t2.sort
AA BB CC DD EE FF aaaa
SS TT UU VV WW XX aaaa
GG HH II JJ KK LL bbbb
MM NN OO PP QQ RR cccc
If the order is important you will need to iterate across the lines of
the first file
PaulC