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

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
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"

Not the answer you're looking for? Browse other questions tagged or ask your own question.