title: Running sed ON a variable in bash script
body: <p>Apologies for a seemingly inane question. But I have spent the whole day trying to figure it out and it drives me up the walls. I'm trying to write a seemingly simple bash script that would take a list of files in the directory from ls, replace part of the file names using sed, get unique names from the list and pass them onto some command. Like so:</p>
  
  <pre><code>inputs=`ls *.ext`
  echo $inputs
  test1_R1.ext  test1_R2.ext  test2_R1.ext  test2_R2.ext
  </code></pre>
  
  <p>Now I would like to put it through sed to replace 1.ext and 2.ext with * to get test1_R* etc. Then I'd like to remove resulting duplicates by running sort -u to arrive to the following $outputs variable:</p>
  
  <pre><code>echo $outputs
  test1_R* test2_R*
  </code></pre>
  
  <p>And pass this onto a command, like so</p>
  
  <pre><code>cat $outputs
  </code></pre>
  
  <p>I can do something like this in a command line:</p>
  
  <pre><code>ls *.ext | sed s/..ext/\*/g | sort -u
  </code></pre>
  
  <p>But if I try to assign the above to a variable in the script it just returns the output from the ls. I have tried several ways to do it: including the whole pipe in the script. Running each command separately and assigning it to a variable, then passing that variable to the next command and writing the outputs to files then passing the file to the next command. But so far none of this managed to achieve what I aimed to. I think my problem lies in (except general cluelessness aroung bash scripting) inability to run seq on a variable within script. There seems to be a lot of advice around in how to pass variables to pattern or replacement string in sed, but they all seem to take files as input. But I understand that it might not be the proper way of doing it anyway. Therefore I would really appreciate if someone could suggest an elegant way to achieve, what I'm trying to.</p>
  
  <p>Many thanks!</p>
  
  <p>Update 2/06/2014</p>
  
  <p>Hi Barmar, thanks for your answer. Can't say it solved the problem, but it helped pin-pointing it. Seems like the problem is in me using the asterisk. I have to say, I'm very puzzled. The actual file names I've got are: </p>
  
  <p><code>test1_R1.fastq.gz test1_R2.fastq.gz test2_R1.fastq.gz test2_R2.fastq.gz</code></p>
  
  <p>If I'm using the code you suggested, which seems to me the right way do to it:</p>
  
  <p><code>ins=$(ls *.fastq.gz | sed 's/..fastq.gz/\*/g' | sort -u)</code></p>
  
  <p>Sed doesn't seem to do anything and I'm getting the output of ls:</p>
  
  <p><code>test1_R1.fastq.gz test1_R2.fastq.gz test2_R1.fastq.gz test2_R2.fastq.gz</code></p>
  
  <p>Now if I replace that backslash with anything else, the sed works, but it also returns whatever character I'm putting in front (or after) the asteriks:</p>
  
  <pre><code>ins=$(ls *.fastq.gz | sed 's/..fastq.gz/"*/g' | sort -u)
  test1_R"* test2_R"*
  </code></pre>
  
  <p>That's odd enough, but surely I can just put an "R" in front of the asteriks and then replace R in the search pattern string, right? Wrong! If I do that whichever way: <code>'s/R..fastq.gz/R*/g'</code> <code>'s/...fastq.gz/R*/g'</code>  <code>'s/[A-Z]..fastq.gz/R*/g'</code> I'm back to the original names! And even if I end up with something like <code>test1_RR* test2_RR*</code> and try to run it through sed again and replace <code>"_R"</code> for <code>"_"</code> or <code>"RR"</code> for <code>"R"</code>, I'm having no luck and I'm back to the original names. And yet I can replace the rest of the file name no problem, just not to get me <code>test1_R*</code> I need.</p>
  
  <p>I have a feeling I should be escaping that * in some very clever way, but nothing I've tried seems to work. Thanks again for your help!</p>