我已经找到了关于如何在两种模式之间进行sed / awk的几个答案,但我还需要仅查找其中包含字符串的特定文本块!
文字范例:
<requirement id = "blabla.1"
slogan = "Handling of blabla"
work-package = "bla444.2"
logical-node = "BLA-C"
level = "System"
>
Bla bla.
</requirement>
<requirement id = "bla.2"
slogan = "Reporting of blabla"
work-package = "bla444.1"
logical-node = "BLA-C"
level = "System"
>
Bla bla bla.
</requirement>
因此,目标是仅获取&之间的文本块,该文本块在工作包中应具有bla444.1!在示例中,这应该只给我最后一个文本块。当然,我想要sed的文件有更多的要求,还有一些需要的工作包,因此不仅会找到sed的最后一个文本块。
sed -e 's/<requirement\(.*\)<\/requirement/\1/' file
上面的sed行将给出所有文本块(要求)。
一件事是该文本块没有固定的行数,但是全部都有工作包!
感谢在广告中。
您可以尝试以下吗?
awk '
/^<requirement/{
if(found && value){
print value
}
found=value=""
}
{
value=(value?value ORS:"")$0
}
/work-package.*bla444.1\"$/{
found=1
}
END{
if(found && value){
print value
}
}
' Input_file
说明:添加以上代码的详细说明。
awk ' ##Starting awk program from here.
/^<requirement/{ ##Checking condition if line starts from string <requirement then do following.
if(found && value){ ##Checking condition if found and value is NOT NULL then do following.
print value ##Printing value(which contains all blocks value, explained further code) here.
}
found=value="" ##Nullifying variables found and value variables here.
}
{
value=(value?value ORS:"")$0 ##Creating variable value whose value is keep concatenating its own value each time cursor comes here.
}
/work-package.*bla444.1\"$/{ ##Checking condition if a line has string work-package till bla444.1 then do following.
found=1 ##Making variable found and setting value to 1, kind of FLAG enabling stuff.
}
END{ ##Starting END block of this awk code here.
if(found && value){ ##Checking condition if found and value is NOT NULL then do following.
print value ##Printing value variable here.
}
}
' Input_file ##Mentioning Input_file name here.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句