Hi all, I have the following set of files:
BMI.0_transf.txt
ALB.0_transf.txt
ALT.0_transf.txt
APOA.0_transf.txt
APOB.0_transf.txt
AST.0_transf.txt
Each file is organised in the following way (I'll use the first file as an example)
ID BMI.0_transf
1000015 0.221001822645847
1000027 -0.67967683719281
1000039 0.693446528768914
1000040 -0.97642697854059
1000053 1.28602376376819
1000064 -2.23464488923183
1000071 1.42851447024267
1000088 -0.919060898470425
1000096 0.169676433701405
Now, I'd like to duplicate the first column for all these files, so as to obtain the following output (still using the first file as an example):
ID ID BMI.0_transf
1000015 1000015 0.221001822645847
1000027 1000027 -0.67967683719281
1000039 1000039 0.693446528768914
1000040 1000040 -0.97642697854059
1000053 1000053 1.28602376376819
1000064 1000064 -2.23464488923183
1000071 1000071 1.42851447024267
1000088 1000088 -0.919060898470425
1000096 1000096 0.169676433701405
Then, I'd like to rename the first column in this way:
FID IID BMI.0_transf
1000015 1000015 0.221001822645847
1000027 1000027 -0.67967683719281
1000039 1000039 0.693446528768914
1000040 1000040 -0.97642697854059
1000053 1000053 1.28602376376819
1000064 1000064 -2.23464488923183
1000071 1000071 1.42851447024267
1000088 1000088 -0.919060898470425
1000096 1000096 0.169676433701405
I'm trying to get exercised in bash, and I've tried to do the following steps:
I've created a file,
names.txt
, containing the name of the second column in the old version of each file (I'll report an example of the content of this file):BMI.0_transf ALB.0_transf ALT.0_transf APOA.0_transf APOB.0_transf AST.0_transf
- I've written the following commands to obtain the wanted output for all the files in my folder:
#!/bin/bash
file_in=".../names.txt"
line=$(head -n $file_in) N=$(echo ${line}) #name to use
for file in *; do awk '{$1=$1 OFS $1} 1' $file | sed -i -e "1 { r"<(echo ' FID IID $N')" d }" $file ; done
However, it didn't work. Any suggestion to fix the code? Thank you so much!