title: Using sort | awk on one column from a csv?
body: <p>Using p.txt:</p>
  
  <pre><code>$cat p.txt
  R 3
  R 4
  S 1
  S 2
  R 1
  T 1
  R 3
  </code></pre>
  
  <p>The following command sorts based on the second column:</p>
  
  <pre><code>$cat p.txt | sort -k2
  R 1
  S 1
  T 1
  S 2
  R 3
  R 3
  R 4
  </code></pre>
  
  <p>The following command removes repeated values in the second column:</p>
  
  <pre><code>$cat p.txt | sort -k2 | awk '!x[$2]++'
  R 1
  S 2
  R 3
  R 4
  </code></pre>
  
  <p>Now inserting a comma for the sapce, we have the following file:</p>
  
  <pre><code>$cat p1.csv
  R,3
  R,4
  S,1
  S,2
  R,1
  T,1
  R,3
  </code></pre>
  
  <p>The following command still sorts based on the second column:</p>
  
  <pre><code>$cat p1.csv | sort -t "," -k2
  R,1
  S,1
  T,1
  S,2
  R,3
  R,3
  R,4
  </code></pre>
  
  <p>Below is NOT the correct output:</p>
  
  <pre><code>$cat p1.csv | sort -t "," -k2 | awk '!x[$2]++'
  R,1
  </code></pre>
  
  <p>Correct output: </p>
  
  <pre><code>R,1
  S,2
  R,3
  R,4
  </code></pre>
  
  <p>Any suggestions?</p>