title: How to edit beginning and ending of file with sed
body: <p>I'm trying to add array brackets at the beginning and end of a file using <code>sed</code> (after first removing the trailing comma at the end of the file) to put all the content of the file in an array.  I'm first using this sed command to remove the last comma from a file</p>
  
  <pre><code>sed '$ s/,$//' "$path"
  </code></pre>
  
  <p>After that, I'm using the middle command below to add array brackets at beginning and ending of file</p>
  
  <pre><code> sed '$ s/,$//' "$path" |   sed 's/^.*$/[&amp;]/' | tee $filename
  </code></pre>
  
  <p>This <code>sed 's/^.*$/[&amp;]/'</code> was supposed to match everything (from beginning to end <code>^$</code>) and then put brackets around the whole match <code>[&amp;]</code> (i.e. as if to make it into an array), but it instead put array brackets around the beginning and end of each line. </p>
  
  <p>Question, how to edit the beginning and ending of a file with sed? </p>
  
  <p>whole script</p>
  
  <pre><code>for path in dirname/* do
     name="${path##*/}"
     sed '$ s/,$//' "$path" | sed 's/^.*$/[&amp;]/' | tee "newdir/$name"
  done
  </code></pre>