1

Input File

Device Robot Drive       Robot                    Drive                Device      Second
Type     Num Index  Type DrNum Status  Comment    Name                 Path        Device Path
robot      0    -    TLD    -       -  -          -                    /dev/sg66
  drive    -    0 hcart2    3      UP  -          IBM.ULT3580-TD5.006  /dev/nst17
  drive    -    1 hcart2    4      UP  -          IBM.ULT3580-TD5.000  /dev/nst16
  drive    -    2 hcart2    5      UP  -          IBM.ULT3580-TD5.001  /dev/nst15
  drive    -    3 hcart2    6      UP  -          IBM.ULT3580-TD5.002  /dev/nst14
  drive    -    4 hcart2    2      UP  -          IBM.ULT3580-TD5.007  /dev/nst13
  drive    -    5 hcart2    1      UP  -          IBM.ULT3580-TD5.008  /dev/nst12
  drive    -    6 hcart2   12    DOWN  -          IBM.ULT3580-TD5.003  /dev/nst11
  drive    -    6 hcart2   12    DOWN  -          IBM.ULT3580-TD5.003  /dev/nst1
  drive    -    7 hcart2   10      UP  -          IBM.ULT3580-TD5.004  /dev/nst10
  drive    -    7 hcart2   10      UP  -          IBM.ULT3580-TD5.004  /dev/nst9
  drive    -    8 hcart2    9      UP  -          IBM.ULT3580-TD5.005  /dev/nst5
  drive    -    8 hcart2    9      UP  -          IBM.ULT3580-TD5.005  /dev/nst6
  drive    -    9 hcart2    8      UP  -          IBM.ULT3580-TD5.009  /dev/nst4
  drive    -    9 hcart2    8    DOWN  -          IBM.ULT3580-TD5.009  /dev/nst2
  drive    -   10 hcart2    7      UP  -          IBM.ULT3580-TD5.010  /dev/nst3
  drive    -   10 hcart2    7      UP  -          IBM.ULT3580-TD5.010  /dev/nst0
robot      1    -    TLD    -       -  -          -                    /dev/sg55
  drive    -   11 hcart2    4      UP  -          IBM.ULT3580-HH5.000  /dev/nst19
  drive    -   12 hcart2    2      UP  -          IBM.ULT3580-HH5.001  /dev/nst18
  drive    -   13 hcart2    1      UP  -          IBM.ULT3580-HH5.002  /dev/nst8
  drive    -   14 hcart2    3      UP  -          IBM.ULT3580-HH5.003  /dev/nst7

I want to find out or rather put in a variable that robot 0 has 11 drives(unique count of field 3) under it and robot 1 has 4

Need to ignore 1st 2 rows which i was doing through awk NR>2

There could be many robot's 0,1,2,3 So i need generic code I tried with awk but couldn't get a definite code.

Output

robot 0 : 11 
robot 1 : 4

and so on

  • Yes field 3 unique entries, which comes out 11 as 6,7,8,9,10 are redundant – Sid Dec 15 '17 at 11:50
  • 1
    could it happen that drive numbers go in arbitrary order, non sorted? like drive 1 , drive 3, drive 7, drive 1 – RomanPerekhrest Dec 15 '17 at 11:51
  • yeah that's fine, i just need the count – Sid Dec 19 '17 at 12:55
3

Awk solution:

awk '/robot/{ 
         if (r){ printf "%s : %d\n", r, c; c=0; delete a } 
         r = $1 OFS $2; 
     }
     /drive/ && !a[$3]++{ c++ }
     END{ printf "%s : %d\n", r, c }' file
  • r = $1 OFS $2 - "robot" name
  • delete a - delete all the elements of the array a. (The following statement provides a portable but nonobvious way to clear out an array: split("", array))
  • c++ - count unique "drive" numbers for a certain "robot"

The output:

robot 0 : 11
robot 1 : 4
  • I got an error in AIX. Syntax Error The source line is 2. The error context is if (r){ printf "%s : %d\n", r, c; c=0; delete a >>> } <<< awk: 0602-502 The statement cannot be correctly parsed. The source line is 2. Syntax Error The source line is 5. – Sid Dec 19 '17 at 12:56
  • Any response for AIX code? Linux it seems fine – Sid Dec 21 '17 at 14:32
  • @Sid, try this line if (r){ printf("%s : %d\n", r, c); c=0; delete a; } instead – RomanPerekhrest Dec 21 '17 at 14:35
  • Nope still this error, Syntax Error The source line is 2. The error context is if (r){ printf("%s : %d\n", r, c); c=0; delete >>> a} <<< awk: 0602-502 The statement cannot be correctly parsed. The source line is 2. Syntax Error The source line is 3. awk: 0602-540 There is a missing } character – Sid Dec 22 '17 at 11:16
  • any suggestions? – Sid Dec 26 '17 at 11:11
0
awk '$1=="robot" && $2 == "1" {x=NR+5} (NR<=x) {print}' test  | awk '{print $3}' | sed '/-/d' |wc -l | awk '{print "robot1:" " "  $0}'

robot1: 4

 awk 'NR >2 {print $0}' test  |sed -n '/robot/,/robot/{x;p;d;};x'| sed '/^$/d' | sed '/robot/d' | awk '{print $3}' | sort | uniq | wc -l | awk '{print "robot0:" " " $1}'

robot0: 11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.