translation of numbers

This is a discussion on translation of numbers within the awk forums in Programming Languages category; Hello I have a file containing a list of numbers: 5 9 4 7 3 2 I have another file which translates the above numbers to another numbering system: 1 101 2 229 3 132 4 112 5 154 and so on... What I need to do is to read each line of the first file and translate the 2 numbers per line to the new numbering system. Of course for this purpose I need to read somehow the whole second file into an array. I do not know if awk is appropriate for this task. I had problems to ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-22-2008, 10:44 AM
hrlusti@gmail.com
Guest
 
Default translation of numbers

Hello

I have a file containing a list of numbers:

5 9
4 7
3 2

I have another file which translates the above numbers to another
numbering system:

1 101
2 229
3 132
4 112
5 154
and so on...

What I need to do is to read each line of the first file and translate
the 2 numbers per line to the new numbering system. Of course for this
purpose I need to read somehow the whole second file into an array.
I do not know if awk is appropriate for this task. I had problems to
call an awk script from within an awk script and also to call a bash
function from an awk script. Does anybody have a suggestion how this
problem can be solved in an elegant way and how such a awk or shell
script would look like.

Thanks for your help.

Hans
Reply With Quote
  #2  
Old 08-22-2008, 11:02 AM
pk
Guest
 
Default Re: translation of numbers

On Friday 22 August 2008 16:44, hrlusti@gmail.com wrote:

> Hello
>
> I have a file containing a list of numbers:
>
> 5 9
> 4 7
> 3 2
>
> I have another file which translates the above numbers to another
> numbering system:
>
> 1 101
> 2 229
> 3 132
> 4 112
> 5 154
> and so on...
>
> What I need to do is to read each line of the first file and translate
> the 2 numbers per line to the new numbering system. Of course for this
> purpose I need to read somehow the whole second file into an array.
> I do not know if awk is appropriate for this task. I had problems to
> call an awk script from within an awk script and also to call a bash
> function from an awk script. Does anybody have a suggestion how this
> problem can be solved in an elegant way and how such a awk or shell
> script would look like.


This should work:

awk 'NR==FNR{a[$1]=$2;next}{$1=a[$1];$2=a[$2];print}' mapfile datafile

assuming the two elements in each line (in both files) are separated by a
space.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:13 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.