Issue
I have a matrix(about 10,000x10,000), and I want to find the column number that contains '0'.
Matrix (test.txt) :
1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 0 1 1 1 1
1 1 1 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
3 2 2 3 3 0 3 2 2 2
3 2 1 3 3 0 3 2 2 0
3 2 2 3 3 2 3 2 2 2
1 1 1 1 1 1 1 1 1 1
Output (example) :
2 4 6 10
I am new to LINUX SHELL, and have not found much in similar examples. Any help would be much appreciated!!
I just know how to find the row number using code: grep -nw '0' test.txt|cut -f1 -d':'
, Maybe I can transpose the matrix first(like this)? And then use the code above, right? Is there an easier way to do it?
Solution
Using any awk in any shell on every Unix box:
$ awk '
/(^| )0( |$)/ {
for ( i=1; i<=NF; i++ ) {
if ( $i == 0 ) {
cols[i]
}
}
}
END {
for ( i in cols ) {
printf "%s%d", sep, i
sep = OFS
}
print ""
}
' file
2 4 6 10
The output above is not guaranteed to be in numeric (or any other) order due to the loop using the in
operator, see https://www.gnu.org/software/gawk/manual/gawk.html#Scanning-an-Array for details.
If you need the field numbers printed in increasing numeric order then change the script to the very slightly slower:
awk '
/(^| )0( |$)/ {
for ( i=1; i<=NF; i++ ) {
if ( $i == 0 ) {
cols[i]
}
}
}
END {
for ( i=1; i<=NF; i++ ) {
if ( i in cols ) {
printf "%s%d", sep, i
sep = OFS
}
}
print ""
}
' file
Answered By - Ed Morton Answer Checked By - Dawn Plyler (WPSolving Volunteer)