-1

I have a dataset in a text file (dataframe.txt), the header of the file is the variable name in numbers. The meaning of the numbers (the actual name of the variable) is stored in another file (variables.txt). For example (edited for simplicity:

> head dataframe.txt 
123 456 789
1    2   3
4    5   6
7    8   9

> variables.txt
123 A
456 B
789 C

I would like to change the header of the dataframe.txt to the names that are stored in the variables.txt file. The link between numbers and name needs to stay the same. The desired output would be:

> head dataframe.txt 
A    B   C
1    2   3
4    5   6
7    8   9

Is there a way to change the header according to another file that links the numbers with the name? I can imagine that a few lines of awk could do the trick..

Thank you!

philipovic
  • 59
  • 8

2 Answers2

1

Here is a simple bash script for your scenario:

filename: script.bash

#!/bin/bash
while read -r line || [[ -n "$line" ]]; do
    sed -i -r '1s/\b'"$(awk '{print $1}' <<< "$line")"'\b/'"$(awk '{print $2}' <<< "$line")"'/g' dataframe.txt
done < variables.txt

Output:

$ cat dataframe.txt 
123 456 789
1    2   3
4    5   6
7    8   9
force@force-virtual-machine:~/temp2$ cat variables.txt 
123 A
456 B
789 C
$ ./script.bash 
$ cat dataframe.txt 
A B C
1    2   3
4    5   6
7    8   9
$
riteshtch
  • 8,629
  • 4
  • 25
  • 38
  • Read http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice to understand why using a shell loop just to manipulate text is always the wrong approach. – Ed Morton Apr 04 '16 at 12:49
  • 1
    @EdMorton Thanks .. will keep that in mind – riteshtch Apr 05 '16 at 02:02
1
$ awk 'NR==FNR{a[$1]=$2;next} FNR==1{for (i=1;i<=NF;i++) $i=a[$i]} 1' variables.txt dataframe.txt | column -t
A  B  C
1  2  3
4  5  6
7  8  9
Ed Morton
  • 188,023
  • 17
  • 78
  • 185