Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

awk

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.

like image 397
gafm Avatar asked Jan 22 '26 21:01

gafm


1 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)}'
like image 136
Inian Avatar answered Jan 26 '26 03:01

Inian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!