title: iterate over columns and only keep (print) columns in which all values match - AWK
body: <p>I would like to iterate over columns in a file and print/retain the column only if all values are identical. The script would continue from the 1st column to the 2nd, etc until a mismatch among column values (strings) is found, then the loop would break and only the columns with matching values (within a given column) are printed.</p>
  <p>each column  ($i) could be tested for duplicates with the below code, but I'm struggling to figure out how to put this together in a loop:</p>
  <pre><code>cut -f&quot;$i&quot; -d &quot; &quot; | sort -u&gt;tmpf; if [ $(wc -l &lt; tmpf) = &quot;1&quot; ];
  </code></pre>
  <p>Here is an example of the dataset Im working with:</p>
  <pre><code>superkingdom:Eukaryota phylum:Arthropoda class:Insecta order:Diptera family:Culicidae genus:Anopheles species;annularis
  superkingdom:Eukaryota phylum:Arthropoda class:Insecta order:Diptera family:Culicidae genus:Anopheles species;dirus
  superkingdom:Eukaryota phylum:Arthropoda class:Insecta order:Diptera family:Culicidae genus:Anopheles species;dirus
  superkingdom:Eukaryota phylum:Arthropoda class:Branchiopoda order:Anostraca family:Thamnocephalidae genus:Branchinella species;pinnata
  superkingdom:Eukaryota phylum:Arthropoda class:Insecta order:Diptera family:Culicidae genus:Culex species;hayashii
  superkingdom:Eukaryota phylum:Arthropoda class:Branchiopoda order:Diplostraca family:Daphniidae genus:Daphnia species;ambigua
  superkingdom:Eukaryota phylum:Arthropoda class:Branchiopoda order:Diplostraca family:Daphniidae genus:Daphnia species;ambigua
  superkingdom:Eukaryota phylum:Arthropoda class:Branchiopoda order:Diplostraca family:Daphniidae genus:Daphnia species;carinata
  </code></pre>
  <p>Iterating over the columns (sep by &quot; &quot;), the first two columns match across all rows, but then the 3rd column (class) does not, so the loop would stop there and only print the first two fields , e.g.</p>
  <pre><code>superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  superkingdom:Eukaryota phylum:Arthropoda
  </code></pre>
  <p>Basically, Id like to keep/print columns that have identical values, and not keep/print columns that have multiple (non identical) values.</p>
  <p>The script would start in column/field 1 and test if all values are the same (comparing strings): if yes (as is the case in example data), then move on to column 2. Test if all values are the same in column 2 (they are), so move on to column 3. Test if all values are the same in column 3 (they are not). So, stop loop/break, and only print previous columns that had identical values.</p>
  <p>The idea is to iterate over the fields/columns in the file and print columns up to where there is a mismatch - with some place holder code for the 'for loop' :</p>
  <pre><code>for ... do cut -f&quot;$i&quot; -d &quot; &quot; | sort -u&gt;tmpf; if [ $(wc -l &lt; tmpf) = &quot;1&quot; ]; then awk '{printf &quot;%s ;&quot;, $0}' tmpf; else break; fi; done
  </code></pre>
  <p>Any help would be much appreciated!</p>
  <p>LP</p>