我有一个日志文件,有以下几行;

Time = 0.9998

DILUBiCGStab:  Solving for Ux, Initial residual = 1.99533113036e-06, Final residual = 2.96249561335e-10, No Iterations 1
DILUBiCGStab:  Solving for Uy, Initial residual = 0.000417321531609, Final residual = 7.45521650555e-10, No Iterations 2
DILUBiCGStab:  Solving for Uz, Initial residual = 0.000101783532684, Final residual = 2.22925230463e-10, No Iterations 2
PISO corrector = 1
GAMG:  Solving for p, Initial residual = 0.0176383087802, Final residual = 0.000128354492605, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.00157044214071, Final residual = 6.05258709305e-06, No Iterations 3
time step continuity errors : sum local = 1.07897616229e-11, global = -1.21038993529e-13, cumulative = 3.09373543327e-11
PISO corrector = 2
GAMG:  Solving for p, Initial residual = 0.00396386732982, Final residual = 1.39668381997e-05, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.000387895480416, Final residual = 2.61276578576e-06, No Iterations 2
time step continuity errors : sum local = 4.66055831479e-12, global = -9.01547272112e-14, cumulative = 3.08471996055e-11
PISO corrector = 3
GAMG:  Solving for p, Initial residual = 0.0014865117999, Final residual = 5.92873264058e-06, No Iterations 2
PISO: converged in 3 correctors
GAMG:  Solving for p, Initial residual = 0.00018575923819, Final residual = 1.19602052482e-06, No Iterations 2
time step continuity errors : sum local = 2.13399696121e-12, global = -5.81036463017e-14, cumulative = 3.07890959592e-11
DILUBiCGStab:  Solving for nuTilda, Initial residual = 1.76763063985e-06, Final residual = 1.29954103535e-10, No Iterations 1
ExecutionTime = 22441.72 s  ClockTime = 22620 s
deltaT
Courant Number mean: 0.819402719286 max: 27.6501988889

FunctionObject:cflBlendingFactor:blendingFactors_regAir_1
60 faces with Courant number based blending for field U
U maximum, minimum and mean blending factors: 1, 0, 0.99151536773

Time = 1

DILUBiCGStab:  Solving for Ux, Initial residual = 1.99417672249e-06, Final residual = 2.96731829464e-10, No Iterations 1
DILUBiCGStab:  Solving for Uy, Initial residual = 0.000410917566734, Final residual = 7.49859275563e-10, No Iterations 2
DILUBiCGStab:  Solving for Uz, Initial residual = 0.000102377986855, Final residual = 2.29772350504e-10, No Iterations 2
PISO corrector = 1
GAMG:  Solving for p, Initial residual = 0.0176826205863, Final residual = 0.000128293418219, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.0015679277464, Final residual = 5.63358605766e-06, No Iterations 3
time step continuity errors : sum local = 1.00341782615e-11, global = -1.18362574514e-13, cumulative = 3.06707333847e-11
PISO corrector = 2
GAMG:  Solving for p, Initial residual = 0.00396565210757, Final residual = 1.3750596194e-05, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.000389224800858, Final residual = 2.48748848039e-06, No Iterations 2
time step continuity errors : sum local = 4.43399728322e-12, global = -9.30058928658e-14, cumulative = 3.05777274918e-11
PISO corrector = 3
GAMG:  Solving for p, Initial residual = 0.00148579017778, Final residual = 5.81035754297e-06, No Iterations 2
PISO: converged in 3 correctors
GAMG:  Solving for p, Initial residual = 0.000185729750804, Final residual = 1.1427315649e-06, No Iterations 2
time step continuity errors : sum local = 2.03756129196e-12, global = -6.12459431033e-14, cumulative = 3.05164815487e-11
DILUBiCGStab:  Solving for nuTilda, Initial residual = 1.76831276642e-06, Final residual = 1.28999442413e-10, No Iterations 1
ExecutionTime = 22451 s  ClockTime = 22630 s

所以,我只需要能够读取0.0176383087802和0.0176826205863的值。我的尝试如下;
cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N' | tr -d ',' > p_tmp.dat

但是,这总是导致读取所有值。我一直在读关于sed命令的文章,但我似乎不知道是什么原因。任何人都可以提供一些关于如何使它每次读取第一个值并输出到dat文件的见解。

最佳答案

考虑到您需要读取找到的字符串行的第一个值。你能试着跟着我走吗。

awk '/Solving for p/ && match($0,/=[^,]*/) && ++count==1{print substr($0,RSTART+2,RLENGTH-2)}' Input_file

说明:
awk '                                                        ##Starting awk program from here.
/Solving for p/ && match($0,/=[^,]*/) && ++count==1{         ##Checking condition if a line has string Solving for p and then matching a regex to match everything from first occurrence of = till first occurrence of , THEN checking condition if variable count value is equal to 1 then do following.
  print substr($0,RSTART+2,RLENGTH-2)                        ##Printing sub-string of line starting from RSTART+1 value to till valie of RLENGTH-2 value.
}
'  Input_file                                                ##Mentioning Input_file name here.

关于linux - sed命令grep特定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58742301/

10-12 21:07