Together:
$ awk '{for (i=1; i<=NF; i++) {if ($i) {print; next}}}' file | awk '{l=NR; c=NF; for (i=1; i<=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i<=l; i++) {for (j=1; j<=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n"}}'
This does a line check:
$ awk '{for (i=1; i<=NF; i++) {if ($i) {print; next}}}' file 1 0 1 1 1 0 1 0 1 0 0 1
It moves along all the fields of the line. If any of them is "true" (this means not 0), it prints a line ( print ) and breaks on the next line ( next ).
This does a column check:
$ awk '{l=NR; c=NF; for (i=1; i<=c; i++) { a[l,i]=$i; if ($i) e[i]++ }} END{ for (i=1; i<=l; i++){ for (j=1; j<=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n" } }'
Basically, it stores all the data in an array a , l number of rows, c number of columns. e is to save the array if the column has any value other than 0 or not. Then it cycles through and prints all the fields only when the index of the array e , which means that if this column has any nonzero value.
Test
$ 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<=NF; i++) {if ($i) {print; next}}}' a | awk '{l=NR; c=NF; for (i=1; i<=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i<=l; i++) {for (j=1; j<=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
previous entry:
$ 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<=NF; i++) {if ($i) {print; next}}}' file | awk '{l=NR; c=NF; for (i=1; i<=c; i++) {a[l,i]=$i; if ($i) e[i]++}} END{for (i=1; i<=l; i++) {for (j=1; j<=c; j++) {if (e[j]) printf "%d ",a[i,j] } printf "\n"}}' 1 1 1 1 1 0 1 0 1
source share