bash:如何从定义的字符串后面的部分检索以Cd和Cl开头的两行?

时间:2017-09-09 12:03:23

标签: bash

我有一个由数千个标准部分组成的模拟报告,如下所示:

Time = 4

noneexPBiCGStab:  Solving for Ux, Initial residual = 0.000981702, Final residual = 4.61883e-11, No Iterations 7
noneexPBiCGStab:  Solving for Uz, Initial residual = 0.00156479, Final residual = 6.19981e-10, No Iterations 7
noneexPBiCGStab:  Solving for p, Initial residual = 0.813427, Final residual = 83.3066, No Iterations 100
time step continuity errors : sum local = 1.47819e-07, global = 1.78896e-18, cumulative = 4.34085e-18
noneexPBiCGStab:  Solving for omega, Initial residual = 0.0410561, Final residual = 1.10415e-12, No Iterations 7
noneexPBiCGStab:  Solving for kl, Initial residual = 0.000179256, Final residual = 3.03264e-08, No Iterations 6
bounding kl, min: -2.43337e-17 max: 0.0157945 average: 5.966e-05
noneexPBiCGStab:  Solving for kt, Initial residual = 0.0586136, Final residual = 3.49538e-06, No Iterations 4
bounding kt, min: 0 max: 0.000296502 average: 4.84351e-05
ExecutionTime = 13.22 s  ClockTime = 2 s

forces forces write:
    sum of forces:
        pressure : (-58.2965 -2.81001e-17 -93.1677)
        viscous  : (0.91475 1.49625e-17 -0.144504)
        porous   : (0 0 0)
    sum of moments:
        pressure : (-0.465839 30.0683 0.291483)
        viscous  : (-0.000722522 -0.0253209 -0.00457375)
        porous   : (0 0 0)

forceCoeffs forceCoeffs write:
    Cm    = 1.50215
    Cd    = -2.86909
    Cl    = -4.66561
    Cl(f) = -0.830658
    Cl(r) = -3.83495

Time = 5

noneexPBiCGStab:  Solving for Ux, Initial residual = 0.00714956, Final residual = 1.87826e-10, No Iterations 7
noneexPBiCGStab:  Solving for Uz, Initial residual = 0.0115055, Final residual = 9.65481e-10, No Iterations 7
noneexPBiCGStab:  Solving for p, Initial residual = 0.957089, Final residual = 12.1321, No Iterations 100
time step continuity errors : sum local = 7.38606e-08, global = -5.69287e-18, cumulative = -1.35202e-18
noneexPBiCGStab:  Solving for omega, Initial residual = 0.0260988, Final residual = 3.68372e-11, No Iterations 6
noneexPBiCGStab:  Solving for kl, Initial residual = 0.000118206, Final residual = 9.28461e-09, No Iterations 6
bounding kl, min: -1.60698e-07 max: 0.0193878 average: 7.74828e-05
noneexPBiCGStab:  Solving for kt, Initial residual = 0.0414096, Final residual = 3.85985e-06, No Iterations 4
bounding kt, min: 0 max: 0.0009156 average: 4.8464e-05
ExecutionTime = 16.52 s  ClockTime = 3 s

forces forces write:
    sum of forces:
        pressure : (-25.9825 -1.81748e-17 -39.0016)
        viscous  : (1.0183 3.13511e-19 0.0527919)
        porous   : (0 0 0)
    sum of moments:
        pressure : (-0.195008 8.3975 0.129912)
        viscous  : (0.00026396 -0.0563941 -0.00509149)
        porous   : (0 0 0)

forceCoeffs forceCoeffs write:
    Cm    = 0.417055
    Cd    = -1.24821
    Cl    = -1.94744
    Cl(f) = -0.556666
    Cl(r) = -1.39078

Time = 6
...

如果您愿意,可能有10000个以Time = x开头的部分,其中x是“部分编号”。

我想从定义的部分中检索/回显以Cd和Cl开头的行。

示例:

我想从以Time = 5开头的部分开始以Cd和Cl开头的行,输出应为:

Time = 5
    Cd    = -1.24821
    Cl    = -1.94744

怎么做?

2 个答案:

答案 0 :(得分:0)

对于这项工作,最好使用像awk这样的高级工具。

使用gnu awk你可以做这样的事情:

$ sect=5;awk "/^Time/{p=0};/^Time = $sect/{print;p=1};p && (/\sCd/||/^\s+Cl\s+/)" <(echo "$f1")
Time = 5
    Cd    = -1.24821
    Cl    = -1.94744

答案 1 :(得分:0)

使用 awk
如果字符串&#34;时间= 5&#34;找到,设置t = 1并打印包含&#34; Cd&#34;的连续字符串。和&#34; Cl&#34;在递增i时。一旦i达到2(即&#34的字符串; Cl&#34;已打印),退出(为了更好的性能)。

awk '$0~/Time = 5/{print $0; t=1;i=0} t==1 && ($0~/Cd / || $0~/Cl /){print $0; if(++i==2){exit}}' file

输出:

Time = 5
    Cd    = -1.24821
    Cl    = -1.94744

注意:如果您有多种可能性,其中&#34;时间= 5&#34;你可以修改awk如下: 即代替EXIT,设置i = 0和t = 0进行进一步的迭代

awk '$0~/Time = 5/{print $0; t=1;i=0} t==1 && ($0~/Cd / || $0~/Cl /){print $0; if(++i==2){i=0; t=0}}' file

您也可以使用 grep

 grep -E "Time = 5|Cd | Cl " l1 | grep "Time = 5" -A2 file

这两项的输出将是:

Time = 5
    Cd    = -1.24821
    Cl    = -1.94744
Time = 5
    Cd    = -1.50000
    Cl    = -1.60000