【发布时间】:2013-03-27 08:24:33
【问题描述】:
我知道这个问题How to find patterns across multiple lines using grep? 但我认为我的问题更复杂。所以我需要帮助。
我有一个字典文件BCFile as
boundary
{
inlet
{
type fixedValue;
value uniform (5 0 0);
}
outlet
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
....
}
我正在编写一个脚本,以便打印出inlet 边界条件fixedValue 和outlet 边界条件inletOutlet。
如果我使用cat BCFile | grep "type" | awk '{printf $2}' | tr -d ";",它将不起作用,因为关键字type 多次出现。
如果我使用awk -v RS='}' '/inlet/ { print $4 }' BCFile,它也不起作用,因为关键字inlet也出现了很多次。
我需要一种方法来查找首先搜索关键字 inlet 然后搜索 最接近 { 和 } 的模式。
有谁知道如何巧妙地做到这一点?
【问题讨论】:
-
查找具有
flag变量的 awk 解决方案。每周都有几个出现在这里。 IE。'/type/{t=1};/value/{v=1}; {t && v}' file(可能不完全正确,因此作为评论发布)。祝你好运。