Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All together:</p> <pre><code>$ awk '{for (i=1; i&lt;=NF; i++) {if ($i) {print; next}}}' file | awk '{l=NR; c=NF; for (i=1; i&lt;=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i&lt;=l; i++) {for (j=1; j&lt;=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n"}}' </code></pre> <p>This makes the row checking:</p> <pre><code>$ awk '{for (i=1; i&lt;=NF; i++) {if ($i) {print; next}}}' file 1 0 1 1 1 0 1 0 1 0 0 1 </code></pre> <p>It loops through all the fields of the line. If any of them are "true" (meaning not 0), it prints the line (<code>print</code>) and breaks to next line (<code>next</code>).</p> <p>This makes the column checking:</p> <pre><code>$ awk '{l=NR; c=NF; for (i=1; i&lt;=c; i++) { a[l,i]=$i; if ($i) e[i]++ }} END{ for (i=1; i&lt;=l; i++){ for (j=1; j&lt;=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n" } }' </code></pre> <p>It basically saves all the data in the <code>a</code> array, <code>l</code> number of lines, <code>c</code> number of columns. <code>e</code> is an array saving if a column has any value different from 0 or not. Then it loops and prints all fields just when <code>e</code> array index is set, meaning if that column has any non-zero value.</p> <h3>Test</h3> <pre><code>$ cat a 1 0 1 0 1 0 0 0 0 0 1 1 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 1 $ awk '{for (i=1; i&lt;=NF; i++) {if ($i) {print; next}}}' a | awk '{l=NR; c=NF; for (i=1; i&lt;=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i&lt;=l; i++) {for (j=1; j&lt;=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n"}}' 1 0 1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 0 1 1 </code></pre> <p>previous input:</p> <pre><code>$ cat file 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 $ awk '{for (i=1; i&lt;=NF; i++) {if ($i) {print; next}}}' file | awk '{l=NR; c=NF; for (i=1; i&lt;=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i&lt;=l; i++) {for (j=1; j&lt;=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n"}}' 1 1 1 1 1 0 1 0 1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload