I am trying to replace an existing header in a file based on the contents of another file.
File1 - names.in:
name1
name2
name3
name4
name5
File2- Datafile.tsv:
chr begin end genes genes genes genes genes
File2 contains more lines but I want to replace each occurrence of the string genes in the first line with the successive entries from file1, so that the header becomes:
chr begin end name1.Corrected name2.Corrected name3.Corrected name4.Corrected name5.Corrected
I tried the following script:
genenames=$1
sed -i -e "1s/\tgenes/\t$genenames\.Corrected/g" Datafile.tsv
But when I run it like so:
./sed.sh names.in
I get the output as:
chr begin end .in.Corrected .in.Corrected .in.Corrected .in.Corrected .in.Corrected
I can understand that the script does not read from the input.
Ntimes just to replaceNoccurrences ofgenes. Also, it will substitute on every line that matches not only on the first line. – don_crissti Sep 21 '15 at 13:32