1

I am looking for a specific value while searching a text file, but with grep I am unable to pull that value. with -head 50, I can see the first 50 lines.

with cat and filename, I see the output as below:

 1 9 1 :     I D                             =   H o s t I n i t i a t o r _ 2 0 0
             H o s t                         =   H o s t _ 9 0
             U I D                           =   2 0 : 0 0 : 0 C : 2 9 : E F : 5 D : 2 B : D 1 : 2 0 : 0 1 : 0 C : 2 9 : E F : 5 D : 2 B : D 1
             I n i t i a t o r   t y p e     =   f c
             P o r t s   l o g g e d   i n   =   s p b _ i o m _ 1 _ f c 3
             I g n o r e d                   =   n o
             H e a l t h   S t a t e         =   O K   ( 5 )
             H e a l t h   D e t a i l s     =   " T h e   c o m p o n e n t   i s   o p e r a t i n g   n o r m a l l y .   N o   a c t i o n   i s   r e q u i r e d . "
             C H A P   u s e r s             =

 1 9 2 :     I D                             =   H o s t I n i t i a t o r _ 2 0 1
             H o s t                         =   H o s t _ 9 2
             U I D                           =   2 0 : 0 0 : B 8 : C B : 2 9 : 2 D : C F : 4 A : 2 0 : 0 1 : B 8 : C B : 2 9 : 2 D : C F : 4 A
             I n i t i a t o r   t y p e     =   f c
             P o r t s   l o g g e d   i n   =   s p b _ i o m _ 1 _ f c 3
             I g n o r e d                   =   n o
             H e a l t h   S t a t e         =   O K   ( 5 )
             H e a l t h   D e t a i l s     =   " T h e   c o m p o n e n t   i s   o p e r a t i n g   n o r m a l l y .   N o   a c t i o n   i s   r e q u i r e d . "
             C H A P   u s e r s             =

 1 9 3 :     I D                             =   H o s t I n i t i a t o r _ 2 0 2
             H o s t                         =   H o s t _ 8 6
             U I D                           =   2 0 : 0 0 : B 8 : C B : 2 9 : 2 D : D 0 : 0 1 : 2 0 : 0 1 : B 8 : C B : 2 9 : 2 D : D 0 : 0 1
             I n i t i a t o r   t y p e     =   f c
             P o r t s   l o g g e d   i n   =   s p b _ i o m _ 1 _ f c 3
             I g n o r e d                   =   n o
             H e a l t h   S t a t e         =   O K   ( 5 )
             H e a l t h   D e t a i l s     =   " T h e   c o m p o n e n t   i s   o p e r a t i n g   n o r m a l l y .   N o   a c t i o n   i s   r e q u i r e d . "
             C H A P   u s e r s             =

 1 9 4 :     I D                             =   H o s t I n i t i a t o r _ 2 0 3
             H o s t                         =   H o s t _ 8 5
             U I D                           =   2 0 : 0 0 : 0 C : 2 9 : E F : 5 D : 2 A : 6 3 : 2 0 : 0 1 : 0 C : 2 9 : E F : 5 D : 2 A : 6 3
             I n i t i a t o r   t y p e     =   f c
             P o r t s   l o g g e d   i n   =   s p b _ i o m _ 1 _ f c 3
             I g n o r e d                   =   n o
             H e a l t h   S t a t e         =   O K   ( 5 )
             H e a l t h   D e t a i l s     =   " T h e   c o m p o n e n t   i s   o p e r a t i n g   n o r m a l l y .   N o   a c t i o n   i s   r e q u i r e d . "
             C H A P   u s e r s             =

whereas with vi editor, I see them as below

6:    ID              = HostInitiator_6
      Host            = Host_2
      UID             = 20:00:58:8A:5A:8A:D3:12:20:01:58:8A:5A:8A:D3:12
      Initiator type  = fc
      Ports logged in = spa_iom_1_fc3
      Ignored         = no
      Health State    = OK (5)
      Health Details  = "The component is operating normally. No action is required."
      CHAP users      =

7:    ID              = HostInitiator_7
      Host            = Host_6
      UID             = 20:00:58:8A:5A:8A:D3:32:20:01:58:8A:5A:8A:D3:32
      Initiator type  = fc
      Ports logged in = spa_iom_1_fc3
      Ignored         = no
      Health State    = OK (5)
      Health Details  = "The component is operating normally. No action is required."
      CHAP users      =

8:    ID              = HostInitiator_8
      Host            = Host_1
      UID             = 20:00:D0:94:66:2F:0A:BA:20:01:D0:94:66:2F:0A:BA
      Initiator type  = fc
      Ports logged in = spa_iom_1_fc3
      Ignored         = no
      Health State    = OK (5)
      Health Details  = "The component is operating normally. No action is required."
      CHAP users      =
94:   ID              = HostInitiator_94
      Host            = Host_49
      UID             = 20:00:00:25:B5:00:00:CE:20:00:00:25:B5:0A:00:CE
      Initiator type  = fc
      Ports logged in =
      Ignored         = no
      Health State    = Degraded/Warning (10)
      Health Details  = "The initiator does not have any logged-in initiator paths. Check the connection between the initiator and the storage system. 

I want to grep and find the Host Name and Initiator ID for those with Health State marked as degraded

3
  • 1
    Please include the command you run. You're doing something that inserts a space between all characters. Oct 14, 2022 at 5:55
  • 1
    Please run cat -A filename and show us the output
    – pLumo
    Oct 14, 2022 at 6:50
  • Or, perhaps shorter, please show us the output of head -n1 file | od -c. We need to see what hidden characters you have that cause the file to be displayed this way.
    – terdon
    Oct 14, 2022 at 9:14

1 Answer 1

-2

We can use a combination of while, if and cut to fetch the result. I am assuming file name as health_data.

Following goes the bash script:

# ! /bin/bash

while read line
do
  if [[ $line == *[[:space:]]"ID"[[:space:]]* ]]
  then
    ID=$(cut -d "=" -f2- <<< $line)
  elif [[ $line == *"Host"[[:space:]]* ]]
  then
    HOST=$(cut -d "=" -f2- <<< $line)
  elif [[ $line == *"Degraded/Warning"[[:space:]]* ]]
  then
    echo $ID
    echo $HOST
  fi
done < health_data

So here I am using while loop iterating over all the lines of the file health_data. I am storing ID and Host based on string matching and slicing. When I encounter Degraded/Warning in the line, I am spitting out both the ID and the Host.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .