0

Say I have a BCFile having the following contents:

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)

In order to print out the type of outlet boundary condition, that is inletOutlet, I thought I could use,

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

But the problem now is in using grep there are so many type keyword appeared. So is there a way to first detect the word outlet, then search and grep the contents between {}? Thanks!

2 Answers 2

1

AWK is pretty powerful. For example if you set the record separator to } each block becomes a record of its own. Then you just print the matching record:

$ awk -v RS='}' '/outlet/ { print $0 }' file
outlet
{
    type            inletOutlet;
    inletValue      $internalField;
    value           $internalField;
Sign up to request clarification or add additional context in comments.

2 Comments

But what if it is inlet, since keyword inlet occurs many times. This method won't work any more. :(
There's more than one way to do it: you can change the match condition from /outlet/ to /inlet\n{/, or $1 ~ /inlet/, or /^inlet/
0

How about grep -A 5 'outlet' BCFile | grep 'type' | awk '{printf $2}' | tr -d ";"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.