![](/img/trans.png)
[英]Check if tag of an image exists in a docker registry using shell script
[英]Find Substring from XML Tag using Shell Script
我有一个格式为 XML 的文件:
<classes>
<subject lb="Fall Sem 2020">
<name>Operating System</name>
<credit>3</credit>
<type>Theory</type>
<faculty>Prof. XYZ</faculty>
</subject>
<subject lb="Spring Sem 2020">
<name>Web Development</name>
<credit>3</credit>
<type>Lab</type>
</subject>
<subject lb="Fall Sem 2021">
<name>Computer Network</name>
<credit>3</credit>
<type>Theory</type>
<faculty>Prof. ABC</faculty>
</subject>
<subject lb="Spring Sem 2021">
<name>Software Engineering</name>
<credit>3</credit>
<type>Lab</type>
</subject>
</classes>
预期 Output:
Fall Sem 2020
Spring Sem 2020
Fall Sem 2021
Spring Sem 2021
我想提取数组中lb
的值。
我的尝试:我尝试使用sed -n "/lb="/,\/"/p" file.xml
但这个命令没有给我特定 ZD304BA20E96D874141588EEABAC850E3 的值
处理这个问题的正确方法是什么?
在 xml 元素中获取属性值。
如果没有 XML 解析器可用。 使用 GNU sed:
sed -En 's/.* lb="([^"]+)".*/\1/p' file
Output:
Fall Sem 2020 Spring Sem 2020 Fall Sem 2021 Spring Sem 2021
考虑到您没有任何方法使用 xml 工具,请您尝试在awk
中进行操作。
awk '
BEGIN{
OFS=","
}
/<subject lb="/{
match($0,/".*"/)
print substr($0,RSTART+1,RLENGTH-2)
}
' Input_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.