我有一个这样的文件
Name: Test1
Acronym: TEST11
Description: null
Toolkit: false
Tracks:
List:
Name: 1st_test
Acronym: TEST11_1
Created On: 2016-03-22 15:25:27.355
Created By: User.9
Is Default: false
State: State[Inactive]
Capability: Capability[Standard]
No of running instances: 0
Name: 2nd_test
Acronym: TEST11_2
Created On: 2016-03-23 15:07:18.955
Created By: User.9
Is Default: true
State: State[Active]
Capability: Capability[Standard]
No of running instances: 0
Name: 3rd_test
Acronym: TEST11_3
Created On: 2016-03-22 11:54:45.276
Created By: User.9
Is Default: false
State: State[Inactive]
Capability: Capability[Standard]
No of running instances: 1
字符串
我需要一个脚本,打印首字母缩略词(列表下的一个),如果:
- line "Is Default"= false
- 行"State"= Inactive
- line "运行示例数"= 0
从提供的示例文件中,我应该只期望TEST11_1
任何帮助将不胜感激。问候。
3条答案
按热度按时间daupos2t1#
字符串
cgyqldqp2#
由于输入 almost 看起来像YAML,我们可以稍微欺骗一下并将其转换为实际的YAML,然后使用yq:
字符串
sed命令在每个
Name:
前面加上一个-
,前面有两个空格,这可能是最脆弱的部分。然后,yq钻取
.Tracks.List
,应用过滤器,Map到.Acronym
,然后每行打印一个结果。ddarikpa3#
有很多方法可以做到这一点,其中一种方法是使用
grep
来解析文件,然后循环遍历每个列表段。下面是一个完整的例子:
字符串
脚本的输出是
Acronym: TEST11_1
,因为TEST11_1
满足所有三个条件。这些条件在check
函数中定义。我不确定这是否足够清楚,因为脚本变得有点冗长。如果你有任何问题,请随时提问。