0

I have a file which has the following content:

BWA='/software/bwa/bwa-0.7.12/bwa'
SAMTOOLS='/software/samtools/samtools-1.3.1/samtools'

The above tools are on my computer:

  • which bwa => /work/waterhouse_team/miniconda2/envs/arima/bin/bwa and
  • pwd/hic-fq => /scratch/waterhouse_team/benth/dbg2olc-40x/hic-fq

Next, I used those two sed commands:

sed -i.bak 's|/software/bwa/bwa-0.7.12/bwa|$(which bwa)|g' mapping_arima.sh
sed -i.bak 's|/software/samtools/samtools-1.3.1/samtools|$(which samtools)|g' mapping_arima.sh

Unfortunately, as output I received:

BWA='$(which bwa)'
IN_DIR='$(`pwd`)/hic-fq'

How do I have to change the sed commands to get:

  • BWA='/work/waterhouse_team/miniconda2/envs/arima/bin/bwa' and
  • IN_DIR=/scratch/waterhouse_team/benth/dbg2olc-40x/hic-fq

Thank you in advance

user977828
  • 921
  • 4
  • 16
  • 30
  • I would like to get these `BWA='/work/waterhouse_team/miniconda2/envs/arima/bin/bwa'` and `IN_DIR=/scratch/waterhouse_team/benth/dbg2olc-40x/hic-fq` – user977828 Oct 01 '18 at 23:28
  • I also just updated my question. – user977828 Oct 01 '18 at 23:38
  • Related: [How can I use variables in the LHS and RHS of a sed substitution?](https://unix.stackexchange.com/questions/69112/how-can-i-use-variables-in-the-lhs-and-rhs-of-a-sed-substitution) – steeldriver Oct 01 '18 at 23:39

2 Answers2

2

Command substitutions $(…) are not expanded inside single quotes.

You may try:

sed -i.bak "s|/software/bwa/bwa-0.7.12/bwa|$(which bwa)|g" mapping_arima.sh
sed -i.bak "s|/software/samtools/samtools-1.3.1/samtools|$(which samtools)|g" mapping_arima.sh

But, if it is in an script, use:

#!/bin/sh

file=mapping_arima.sh

from01='/software/bwa/bwa-0.7.12/bwa'
to01=$(which bwa)

from02='/software/samtools/samtools-1.3.1/samtools'
to02=$(which samtools)

sed -i.bak "s|$from01|$to01|g" "$file"
sed -i.bak "s|$from02|$to02|g" "$file"
1

You can try these commands instead:

sed -ri.bak "s#software/bwa/bwa-0.7.12/bwa#`which bwa`#g" mapping_arima.sh
sed -ri.bak "s#software/samtools/samtools-1.3.1/samtools#`which samtools`#g" mapping_arima.sh