I have a huge table at which I am trying to change some duplicated column names using sed with first match replacement. For that, I am using an array with the duplicated column names, which I selected manually.
I first tried the sed code with one simple text string, and it worked:
sed '0,/AGE_032/ s//AGE_032.old/' combined.order.allfilter.abund.tsv | head -n1
Then, I tried to replace matches with an isolated element of the array and it is not working.
declare -a oldarr=("AGE_032" "MOLI_032" "OIA_013" "SH-108" "SH-16")
sed '0,/${oldarr[0]}/ s//${oldarr[0]}.old/' combined.order.allfilter.abund.tsv | head -n1
The expected output should be something like this:
AGE_023 AGE_024 AGE_025 AGE_026 AGE_027 AGE_028 AGE_029 AGE_030 AGE_031
AGE_032.old MOLI_029 MOLI_030 MOLI_031 MOLI_032 MOLI_033 SH-107 OIA_013
SH-108 SH-109 SH-110 SH-13 SH-15 SH-16 SH-17 AREN_36 AREN_38 AREN_39
AGE_032 MOLI_032 OIA_013 SH-108 SH-16
Note that AGE_032
, MOLI_032
, OIA_013
, SH-108
and SH-16
appear twice, and only the first match of AGE_032
should be replaced with AGE_032.old
.
Of course, any other code solution for solving the problem will be appreciated.
Clarification: the code must work for replacing the first match of every string inside the array.