0

I want to create multiple files at a time using names from a file list.txt, as well as the text in each files. Suppose I have a list.txt containing some id. numbers, now I want to make separate files using that ids as the name (e.g. EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl, EOG090W00DC_M0.ctl). Also, the contents of the files need to be changed accordingly.

Sample list.txt:

EOG090W002U 
EOG090W00C1 
EOG090W00DC 
EOG090W00DE 
EOG090W00E5
EOG090W00HR 
EOG090W00MH 
EOG090W00MS 
EOG090W00PB 
EOG090W00U4
EOG090W00UK 
EOG090W00WM 
EOG090W00WR

For example the desired content of EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl file will be

seqfile = EOG090W002U_p.phy
treefile = Sametree.txt
outfile = EOG090W002U_M0_mlc

getSE = 0
RateAncestor = 1
Small_Diff = 5e-7
cleandata = 1
fix_blength = 2
method = 0

Or

seqfile = EOG090W00C1_p.phy
treefile = Sametree.txt
outfile = EOG090W00C1_M0_mlc

  getSE = 0
  RateAncestor = 1
  Small_Diff = 5e-7
  cleandata = 1
  fix_blength = 2
  method = 0

Here, seqfile and outfile is going to be changed according to the list.txt but other texts in the file will remain the same.

Thanks

2 Answers2

2

You could use a while loop around a here-document:

while IFS= read -r x; do
  cat << EOF > "${x}_M0.ctl"
  seqfile = ${x}_p.phy
  treefile = Sametree.txt
  outfile = ${x}_M0_mlc

    getSE = 0
    RateAncestor = 1
    Small_Diff = 5e-7
    cleandata = 1
    fix_blength = 2
    method = 0
EOF
done < list.txt

If lines in your list.txt have leading or trailing SPC or TAB characters that should not be interpreted as part of the file name (and you have not otherwise modified the IFS variable), then omit the IFS= assignment before the read command:

while read -r x; do

Or explicitly set it to SPC and TAB:

while IFS=$' \t' read -r x; do

(note that expanding it to other whitespace characters like CR, FF, NBSP... won't work as they don't receive the special IFS whitespace treatment, only SPC, TAB and NL do).

steeldriver
  • 81,074
  • Thanks, but files are created empty. – Debajyoti Kabiraj Jan 23 '20 at 16:28
  • files are empty because of the way cat is directing the output file. it should be cat << EOF >> "${x}_M0.ctl" – BANJOSA Jan 23 '20 at 17:26
  • I'll edit the code above. – BANJOSA Jan 23 '20 at 17:26
  • @BANJOSA why do you believe the redirection needs to be >> rather than >? – steeldriver Jan 23 '20 at 17:54
  • @steeldriver: you are right, it doesn't need it. In my previous testing I've made a typo that resulted in a bad execution. I've modified it as I proposed and worked. Now I've made a test again with your script and worked as expected, except the indentation of the first 3 lines that is now exactly as requested by the OP. Given that is a minor issue, I apologize for the proposed correction and will vote your response up. – BANJOSA Jan 23 '20 at 20:53
  • Thnaks, but thing is that, it is creating space after the variable ${x} like the file name created EOG090W002U _M0.ctl. Same way the inside the file
    seqfile = EOG090W034W _p.phy treefile = Sametree.txt outfile = EOG090W034W _M0_mlc

    So, there is a clear space between the variable and the extension. How can I get rid of this problem.

    Thanks

    – Debajyoti Kabiraj Jan 24 '20 at 04:56
  • That is happening because you have those spaces in the file name list. Remove the spaces after the name in the original list or use the solution below given by @vikram Tidake – BANJOSA Jan 24 '20 at 09:06
  • The script given by @steeldriver giving best result till now though the space is the big issue.. – Debajyoti Kabiraj Jan 24 '20 at 10:32
  • @DebajyotiKabiraj I have suggested a workaround for the whitespace – steeldriver Jan 24 '20 at 10:54
0
#!/bin/bash
tr -d '[:blank:]' < list.txt > outputFile.tmp

for i in $(cat outputFile.tmp)
do
  echo "seqfile = ${i}_p.phy" >> ${i}_M0.ctl
  echo "treefile = Sametree.txt" >> ${i}_M0.ctl
  echo "outfile = ${i}_M0_mlc" >> ${i}_M0.ctl
  echo "" >> ${i}_M0.ctl
  echo "getSE = 0" >> ${i}_M0.ctl
  echo "RateAncestor = 1" >> ${i}_M0.ctl
  echo "Small_Diff = 5e-7" >> ${i}_M0.ctl
  echo "cleandata = 1" >> ${i}_M0.ctl
  echo "fix_blength = 2" >> ${i}_M0.ctl
  echo "method = 0" >> ${i}_M0.ctl
done
exit 0