Another.Chemist
Another.Chemist

Reputation: 2559

AWK and matrices handling

Good day,

I was wondering how to handle (in awk) a square and symmetric matrix to do:

  1. Identify the row with only zeros.
  2. Delete that row and its symmetric column.
  3. Print the resultant matrix.

Input

  1234567
 xabcdefg
1a0101101
2b1010001
3c0101001
4d1010101
5e1001000
6f0000000
7g1111000

Expected output Sixth row and column deleted

  123457
 xabcdeg
1a010111
2b101001
3c010101
4d101011
5e100100
7g111100

Thanks in advance for any clue

Upvotes: 0

Views: 215

Answers (2)

John1024
John1024

Reputation: 113864

$ awk '{row[NR]=$0} $0=='0000000' {bad=NR} END{for (i=1;i<=NR;i=i+1+(i==bad-1)) {print substr(row[i],1,bad-1) substr(row[i],bad+1)}}' input
010111
101001
010101
101011
100100
111100

Upvotes: 0

Kent
Kent

Reputation: 195079

this line should work:

awk  'BEGIN{FS=OFS=""}NR==FNR{if(/^0+$/)z[NR];next}
                      !(FNR in z){for(x in z)$x="";print}' file file

test:

kent$  cat f
0101101
1010001
0101001
1010101
1001000
0000000
1111000

kent$  awk  'BEGIN{FS=OFS=""}NR==FNR{if(/^0+$/)z[NR];next}!(FNR in z){for(x in z)$x="";print}' f f 
010111
101001
010101
101011
100100
111100

it works for multiple "all-zero" rows too:

kent$  cat f
00001
00000
00000
00000
10000

kent$  awk  'BEGIN{FS=OFS=""}NR==FNR{if(/^0+$/)z[NR];next}!(FNR in z){for(x in z)$x="";print}' f f
01
10

Upvotes: 2

Related Questions