Print an entry of length 32 characters in a table with unequal number of columns using awk

Viewed 83

I have a command that displays multiple columns like the example below.

I'm using awk to print $2, $8 and $10. However, there's a portion in a column that is sometimes empty especially $9 making it hard to display the required output.

165 sacwbg01_02_002 0 io_grp5 online 1 X3P_MAS01 60.00GB  striped                       6006076801238543230000000000063S 0 1 empty     0 no 0 1 X3P_MAS01 no no 165 sacwbg01_02_002        scsi
206 a3jd1_2         0 io_grp5 online 1 X3P_MAS01 50.00GB  striped                       600607680123854323000000000001C8 0 1 empty     0 no 0 1 X3P_MAS01 no no 206 a3jd1_2                scsi
275 ws3sasdf        1 io_grp3 online 1 X3P_MAS01 10.00GB  striped   275 ws3sasdf        600707680123854323000000000005B3 0 1 empty     0 no 0 1 X3P_MAS01 no no 275 ws3sasdf        master scsi
276 zz3s_b_004      1 io_grp3 online 1 X3P_MAS01 10.00GB  striped   276 zz3s_b_004      600707680123854323000000000005B9 0 1 empty     0 no 0 1 X3P_MAS01 no no 276 zz3s_b_004      master scsi
277 cas3a_b_005     1 io_grp3 online 1 X3P_MAS01 10.00GB  striped   277 cas3a_b_005     600707680123854323000000000005B2 0 1 empty     0 no 0 1 X3P_MAS01 no no 277 cas3a_b_005     master scsi

The output that i'm looking for is actually like this

sacwbg01_02_002 60.00GB  6006076801238543230000000000063S
a3jd1_2         10.00GB  600707680123854323000000000005B3
ws3sasdf        10.00GB  600707680123854323000000000005B3 
zz3s_b_004      10.00GB  600707680123854323000000000005B9 
cas3a_b_005     10.00GB  600707680123854323000000000005B2 

The command i'm using is this. If condition for $9 and $10 and print if it's equals to 32 because this 6006076801238543230000000000063S is actually 32 strings.

command -nohdr |awk '{if (($9=="32")||($10=="32")) {print $0}}'

Any idea on how can i go about it? Any help would be appreciated. Thank you.

2 Answers

Use a match() function to match that value in the row whose length is 32 characters. Pipe the result to column -t to pretty print the output.

awk 'match($0, /[[:alnum:]]{32}/){ print $2, $8, substr($0, RSTART, RLENGTH)}'

One option would be to check the number of columns before you print, I'm not sure this is the most clean solution, but if your columns are somewhat static, it should to the job:

awk 'BEGIN {OFS="\t"}{ if (NF==23)  print $2, $8, $10; else print $2, $8, $12}' /tmp/tst.txt | column -t

So, if the number of columns (NF==) is 23, print $2, $8, $10, otherwise, print $2, $8, $12

sacwbg01_02_002  60.00GB  6006076801238543230000000000063S
a3jd1_2          50.00GB  600607680123854323000000000001C8
ws3sasdf         10.00GB  600707680123854323000000000005B3
zz3s_b_004       10.00GB  600707680123854323000000000005B9
cas3a_b_005      10.00GB  600707680123854323000000000005B2

Use column -t to create the table

Related