title: How can I fix my Dr Who downloading script?
body: <p>Here is the background to this script:</p>
  <p>A year or two ago I tried to automate downloading Dr Who by mixing Bash Scripting and the <code>get-iplayer</code> program. This was both an exercise in scripting and wanting to watch Dr Who. I've never gotten iPlayer to work, and I'm too forgetful to run <code>get-iplayer</code> each time.</p>
  <p>Here is the code:</p>
  <pre><code>#!/bin/bash
  #Export programme names; make sure PROG is a search term that only brings up the shows you want
  export PROG=&quot;Doctor Who&quot;
  export SHORT=&quot;Dr&quot;
  
  #Get the info you want into some text files, named after the SHORT keyword
  (ls /home/$USER/Videos/iPlayer/&quot;$PROG&quot; | grep Doctor) &gt;/home/$USER/Videos/iPlayer/.Code/&quot;$SHORT&quot;Hist.tmp
  get_iplayer --listformat=&quot;&lt;pid&gt;: &lt;name&gt; - &lt;episode&gt;&quot; --search=&quot;$PROG:&quot; &gt;/home/$USER/Videos/iPlayer/.Code/&quot;$SHORT&quot;Curr.tmp 
  
  #Tidy the first file to get rid of underscores 
  #then egrep to suck out the pid...schluuurp
  
  sed -i 's/_/ /g' ./&quot;$SHORT&quot;Hist.tmp
  #replace the underscores in the file name with spaces
  egrep  -o '[bp]{1}[0-9]{2}[0-9a-z]{4}' ./&quot;$SHORT&quot;Hist.tmp &gt;./&quot;$SHORT&quot;Hist.txt
  rm ./&quot;$SHORT&quot;Hist.tmp
  egrep  -o '[bp]{1}[0-9]{2}[0-9a-z]{4}' ./&quot;$SHORT&quot;Curr.tmp &gt;./&quot;$SHORT&quot;Curr.txt
  rm ./&quot;$SHORT&quot;Curr.tmp
  #'b[0-9a-z]{7}' is the regex for the pid 
  #--I've edited this as the pid regex no longer matches the above
  
  sort ./&quot;$SHORT&quot;Curr.txt -o ./&quot;$SHORT&quot;Curr.txt
  sort ./&quot;$SHORT&quot;Hist.txt -o ./&quot;$SHORT&quot;Hist.txt
  #get things in order
  
  diff ./&quot;$SHORT&quot;Curr.txt ./&quot;$SHORT&quot;Hist.txt &gt; ./&quot;$SHORT&quot;Diff.txt
  rm ./&quot;$SHORT&quot;Curr.txt ./&quot;$SHORT&quot;Hist.txt
  sed -i 's/[^ ]* //1' ./&quot;$SHORT&quot;Diff.txt 
  sed -i '1d' ./&quot;$SHORT&quot;Diff.txt
  sed -i 's/---/\n/g'./&quot;$SHORT&quot;Diff.txt #&lt;---this doesn't work?
  sed '/^$/q' 
  
  #get-iplayer --listformat=&quot;&lt;index&gt;&quot; --field=pid $(cat &quot;$SHORT&quot;Diff.txt |  tr '\n' ' ') &gt;./&quot;$SHORT&quot;Diff.txt
  #sed -i '1,6d' ./&quot;$SHORT&quot;Diff.txt
  
  #sed -i '/^$/,$ d' ./&quot;$SHORT&quot;Diff.txt
  
  #get-iplayer --get $(cat &quot;$SHORT&quot;Diff.txt |  tr '\n' ' ') --output=&quot;/home/$LOGNAME/Videos/iPlayer/$PROG&quot; --force
  </code></pre>
  <p>Now I wouldn't be posting here if this worked. There are a few issues:</p>
  <ul>
  <li>The sed line I pointed out <code>sed -i 's/---/\n/g'./&quot;$SHORT&quot;Diff.txt</code> give me this error<br><code>sed: -e expression #1, char 11: unknown option to </code>s'`</li>
  <li>The <code>diff</code> program doesn't seem to pick out the current <code>pid</code> I don't know what changed since it worked before.</li>
  </ul>
  <p>Is anyone able to help me fix this script?</p>
  <h3>edit</h3>
  <p>As requested
  <code>uname; sed --version</code>
  produces
  <code>Linux</code>
  <code>GNU sed version 4.2.1</code></p>
  <p>And putting <code>set -x</code> in:</p>
  <pre><code>+ export 'PROG=Doctor Who'
  + PROG='Doctor Who'
  + export SHORT=Dr
  + SHORT=Dr
  + ls '/home/craig/Videos/iPlayer/Doctor Who'
  + grep Doctor
  + get_iplayer '--listformat=&lt;pid&gt;: &lt;name&gt; - &lt;episode&gt;' '--search=Doctor Who:'
  
  + sed -i 's/_/ /g' ./DrHist.tmp
  + egrep -o '[bp]{1}[0-9]{2}[0-9a-z]{4}' ./DrHist.tmp
  + rm ./DrHist.tmp
  + egrep -o '[bp]{1}[0-9]{2}[0-9a-z]{4}' ./DrCurr.tmp
  + rm ./DrCurr.tmp
  + sort ./DrCurr.txt -o ./DrCurr.txt
  + sort ./DrHist.txt -o ./DrHist.txt
  + diff ./DrCurr.txt ./DrHist.txt
  + rm ./DrCurr.txt ./DrHist.txt
  + sed -i 's/[^ ]* //1' ./DrDiff.txt
  + sed -i 1d ./DrDiff.txt
  + sed -i 's/---/\n/g./DrDiff.txt'
  sed: -e expression #1, char 11: unknown option to `s'
  + sed '/^$/q'
  </code></pre>
  <p>And the output:</p>
  <pre><code>p00wqr1 &lt;--one file
  
  
  b00sj9q &lt;--the contents of the other file.
  b00sj9s
  b010tb7
  b010y5l
  b0110g4
  b011884
  b011fnd
  b011lqw
  </code></pre>
  <p>The input of the <code>diff</code> is the first line of output and the lines beginning <code>b0...</code>. Why isn't it working when it did before?</p>