| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.