如何使用shell脚本读取用户定义的字典?

时间:2013-04-04 22:46:21

标签: shell grep

说我有一个BCFile,其中包含以下内容:

inlet
{
    type            fixedValue;
    value           uniform (5 0 0);
}
outlet
{
    type            inletOutlet;
    inletValue      $internalField;
    value           $internalField;
}
....
blahblahblah (other boundary condition with the same dictionary format as above)

为了打印outlet边界条件的类型,即inletOutlet,我认为我可以使用,

cat BCFile | grep "type" | awk '{printf $2}'  | tr -d ";"

但现在的问题是在使用grep时出现了很多type个关键字。那么有没有办法首先检测单词outlet,然后在{}之间搜索和grep内容?谢谢!

2 个答案:

答案 0 :(得分:1)

AWK非常强大。例如,如果将记录分隔符设置为},则每个块将成为其自己的记录。然后你只需打印匹配的记录:

$ awk -v RS='}' '/outlet/ { print $0 }' file
outlet
{
    type            inletOutlet;
    inletValue      $internalField;
    value           $internalField;

答案 1 :(得分:0)

怎么样 grep -A 5 'outlet' BCFile | grep 'type' | awk '{printf $2}' | tr -d ";"

相关问题