提取属性值并使用perl剪切字符串

时间:2017-11-10 18:32:07

标签: shell perl

<Student> <test subject="/school/class/twelve/maths.txt"/> <test subject="/school/class/eleven/physics.txt"/> </Student>

我想取“主题”的值,然后从类开始剪切字符串。 例如,输出必须是两个字符串:12 / maths.txt和eleven / physics.txt 有人可以帮助使用shell脚本或perl来帮助吗?

2 个答案:

答案 0 :(得分:1)

轻松xsh,Perl&#39; XML::LibXML的包装:

open file.xml ;
for /Student/test echo substring-after(@subject, '/school/class/') ;

答案 1 :(得分:0)

I have tried in steps. First to get the attribute value, then taking the substring

INPUT=$(sed -n 's/.*subject="\([^"]*\).*/\1/p' *.xml)
echo "the value of subject attribute=" $INPUT

But this result in the output:

the value of subject attribute=/school/class/twelve/maths.txt /school/class/eleven/physics.txt

after this I have taken the substring:

SubString1=$(echo $INPUT| cut -d'/' -f 5)
echo "SubString1=" $SubString1
nospaces=${SubString1## } # remove leading spaces
SubString1=${nospaces%% } # remove trailing spaces
SubString2=$(echo $INPUT| cut -d'/' -f 11)
echo "SubString2=" $SubString2

and the output is SubString1=maths.txt SubString2=physics.txt