title: Run a bash script from an R script
body: <p>So I have this programme samtools that I want to use from cmd line, converting one file to another. It works like this: </p>
  
  <pre><code>bash-4.2$ samtools view filename.bam | awk '{OFS="\t"; print "&gt;"$1"\n"$10}' - &gt; filename.fasta
  </code></pre>
  
  <p>As I want to automate this, I would like to automate it by using an R script. I know you can use system() to run an OS command, but I cannot get it to work by trying </p>
  
  <pre><code>system(samtools view filename.bam | awk '{OFS="\t"; print "&gt;"$1"\n"$10}' - &gt; filename.fasta)
  </code></pre>
  
  <p>Is it just a matter of using regexes to get rid of spaces and stuff so the comma nd argument system(command) is readable? How do I do this? </p>
  
  <p>EDIT:</p>
  
  <blockquote>
    <p>system("samtools view filename.bam | awk '{OFS="\t"; print ">"$1"\n"$10}' - > first_batch_1.fasta")
    Error: unexpected input in "system("samtools view filename.bam | awk '{OFS="\"</p>
  </blockquote>
  
  <p>EDIT2: </p>
  
  <blockquote>
    <p>system("samtools view filename.bam | awk '{OFS=\"\t\"; print \">\"$1\"\n\"$10}' - > filename.fasta") </p>
  </blockquote>
  
  <pre><code>awk: cmd. line:1: {OFS="    "; print "&gt;"$1"
  awk: cmd. line:1:                         ^ unterminated string
  awk: cmd. line:1: {OFS="    "; print "&gt;"$1"
  awk: cmd. line:1:                         ^ syntax error
  &gt; 
  </code></pre>
  
  <p>EDIT3: 
  And the winner is:</p>
  
  <pre><code>system("samtools view filename.bam | awk '{OFS=\"\\t\"; print \"&gt;\"$1\"\\n\"$10}' -&gt; filename.fasta")
  </code></pre>