Score:1

Grep doesn't return any value in the search

pt flag

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

Artur Meinild avatar
vn flag
Please include the command you run. You're doing something that inserts a space between all characters.
pLumo avatar
in flag
Please run `cat -A filename` and show us the output
terdon avatar
cn flag
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.
Score:-2
sk flag

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.

Artur Meinild avatar
vn flag
This is a very creative extrapolation of the question. The OP does not actually state what he needs, but this script is useful nonetheless.
Avijit avatar
sk flag
Thank you, @ArturMeinild.
terdon avatar
cn flag
The script is unfortunately a really bad idea. First, [the shell isn't a good tool for text parsing](https://unix.stackexchange.com/q/169716/22222). Next, you [aren't quoting your variables](https://unix.stackexchange.com/q/171346/22222) which means this will fail in ugly ways [for certain input](https://unix.stackexchange.com/q/131766/22222).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.