0

OS=GNU Linux

Input File

gene_id "STRG.50";
gene_id "STRG.52";
gene_id "STRG.59";

Template for Replacement

STRG.50  AT1G27500
STRG.52  AT2G37985
STRG.59  AT2G33392

Bash

while read bef aft
do
  echo $bef
  echo $aft
  sed -i "s/$bef\";/$aft\";/g" input.txt
done < template

This does not work. Echo lines work correctly, but the input file remains unchanged and sed hangs indefinitely after all echo statements have finished.

  • After you have this working, you should improve this to do all the changes in a single invocation of sed, rather than one sed for each line of the template. – icarus Nov 15 '19 at 0:54
  • Put set -x before the sed line and set +x after the sed line; this will show you exactly what command the shell is trying to execute. – AlexP Nov 15 '19 at 18:48
0

It actually works:

tuhin@tuhin-virtualvbox:temp$ cat input.txt 
gene_id "STRG.50";
gene_id "STRG.52";
gene_id "STRG.59";
tuhin@tuhin-virtualvbox:temp$ cat template 
STRG.50  AT1G27500
STRG.52  AT2G37985
STRG.59  AT2G33392
tuhin@tuhin-virtualvbox:temp$ while read bef aft; do   echo $bef;   echo $aft;   sed -i "s/$bef\";/$aft\";/g" input.txt; done < template 
STRG.50
AT1G27500
STRG.52
AT2G37985
STRG.59
AT2G33392
tuhin@tuhin-virtualvbox:temp$ cat input.txt 
gene_id "AT1G27500";
gene_id "AT2G37985";
gene_id "AT2G33392";
tuhin@tuhin-virtualvbox:temp$ 
| improve this answer | |
  • Interesting. What system are you on? – cryptic0 Nov 14 '19 at 23:08
  • Works for me too....output is exactly as per @Tuhin – bu5hman Nov 15 '19 at 19:17
  • @cryptic0 ubuntu in virtual box (on windows). Here is the output of lsb_release -a: No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.3 LTS Release: 18.04 Codename: bionic – Tuhin Paul Nov 15 '19 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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