我在搜索文本文件时寻找特定值,但使用 grep 无法提取该值。使用 -head 50,我可以看到前 50 行。
使用 cat 和 filename,我看到如下输出:
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 =
而使用 vi 编辑器,我看到如下所示
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.
我想通过 grep 查找健康状态标记为“降级”的主机名和启动器 ID
答案1
while
我们可以使用、if
和的组合cut
来获取结果。我假设文件名为health_data
。
以下是 bash 脚本:
# ! /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
因此,我在这里使用while
循环迭代文件的所有行health_data
。我根据字符串匹配和切片进行存储ID
和切片。当我在行中遇到时,我会同时输出 ID 和 Host。Host
Degraded/Warning