Skip to main content
added 144 characters in body
Source Link

I need to run search/replace function on a large ascii text file. A short excerpt from input file:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "MSTRG.2";
gene_id "MSTRG.3";

The MSTRG string will be replaced with another ID which is present in a template file:

MSTRG.1 AT1G01030
MSTRG.2 AT1G01010
MSTRG.3 AT1G01035

A simple while loop is iterating over each line of the template and making replacements:

while read bef aft
do
  echo "Searching for $bef"
  echo "Replacing with $aft"
  sed "s/$bef/$aft/g" input > output
done < template

What is happening is that the MSTRG.2 and onward entries are being replaced correctly, but MSTRG.1 remains unchanged. The output looks as follows:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "AT1G01010";
gene_id "AT1G01035"; 

Update

Here is what I did in the end.

while read bef aft
do
  sed -i "s/$bef/$aft/g" input
done < template

I need to run search/replace function on a large ascii text file. A short excerpt from input file:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "MSTRG.2";
gene_id "MSTRG.3";

The MSTRG string will be replaced with another ID which is present in a template file:

MSTRG.1 AT1G01030
MSTRG.2 AT1G01010
MSTRG.3 AT1G01035

A simple while loop is iterating over each line of the template and making replacements:

while read bef aft
do
  echo "Searching for $bef"
  echo "Replacing with $aft"
  sed "s/$bef/$aft/g" input > output
done < template

What is happening is that the MSTRG.2 and onward entries are being replaced correctly, but MSTRG.1 remains unchanged. The output looks as follows:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "AT1G01010";
gene_id "AT1G01035"; 

I need to run search/replace function on a large ascii text file. A short excerpt from input file:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "MSTRG.2";
gene_id "MSTRG.3";

The MSTRG string will be replaced with another ID which is present in a template file:

MSTRG.1 AT1G01030
MSTRG.2 AT1G01010
MSTRG.3 AT1G01035

A simple while loop is iterating over each line of the template and making replacements:

while read bef aft
do
  echo "Searching for $bef"
  echo "Replacing with $aft"
  sed "s/$bef/$aft/g" input > output
done < template

What is happening is that the MSTRG.2 and onward entries are being replaced correctly, but MSTRG.1 remains unchanged. The output looks as follows:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "AT1G01010";
gene_id "AT1G01035"; 

Update

Here is what I did in the end.

while read bef aft
do
  sed -i "s/$bef/$aft/g" input
done < template
Source Link

Replace using sed not working as expected

I need to run search/replace function on a large ascii text file. A short excerpt from input file:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "MSTRG.2";
gene_id "MSTRG.3";

The MSTRG string will be replaced with another ID which is present in a template file:

MSTRG.1 AT1G01030
MSTRG.2 AT1G01010
MSTRG.3 AT1G01035

A simple while loop is iterating over each line of the template and making replacements:

while read bef aft
do
  echo "Searching for $bef"
  echo "Replacing with $aft"
  sed "s/$bef/$aft/g" input > output
done < template

What is happening is that the MSTRG.2 and onward entries are being replaced correctly, but MSTRG.1 remains unchanged. The output looks as follows:

gene_id "MSTRG.1";
gene_id "MSTRG.1";
gene_id "AT1G01010";
gene_id "AT1G01035";