Search code examples
awktabular

Iteration to rename column names


I have a table and I would like to rename some of the headers, the table is something like this:

Names rrdx21 rrdx23 rrdy78 rrdg00 rrdw90 ccv34 vvvbue [...] xxtur44
Values 0       1       2      3     4      5     45   [...]   990
[...]
Values100 2     3      45    76     0      0     0            1000

One the other hand I have a list with the the headers/column names that I want to change and the new name I want to give, like this:

rrdx3   sample2
rrdg00  sample23
[...]

As you can imagine I want to keep the order of the headers/columns. It is possible to do it in awk, or with a simple script?


Solution

  • assume both files are spaces separated, give this line a try: (I didn't test, but should go)

    awk 'NR==FNR{d[$1]=$2;next}FNR==1{for(i=1;i<=NF;i++)$i=d[$i]?d[$i]:$i}7' header.txt table.txt