首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sed命令到grep一个特定值。

sed命令到grep一个特定值。
EN

Stack Overflow用户
提问于 2019-11-07 05:12:06
回答 3查看 138关注 0票数 0

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

代码语言:javascript
复制
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。我的尝试如下;

代码语言:javascript
复制
cat log | grep 'Solving for p' | cut -d' ' -f9 | sed -n 'p;N' | tr -d ',' > p_tmp.dat

但是,这总是会导致读取所有的值。我一直在读有关sed命令的文章,但是我似乎没有因为任何原因而四处走动。任何人都可以了解如何让它每次读取第一个值,并输出到dat文件。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-11-07 10:34:36

不确定为什么要强调使用sed命令来执行一些其他命令可以轻松完成的任务。sed主要用于查找和替换,但似乎您希望实现其他目标。

如果您想获取第一个记录值,可以使用下面的方法。

代码语言:javascript
复制
 [cloudera@quickstart sub1]$ cat a.txt 
    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


[cloudera@quickstart sub1]$ cat a.txt | sed -n '/PISO corrector = 1/{n;P;}' | cut -d' ' -f9 | tr -d ','
0.0176383087802
0.0176826205863
票数 0
EN

Stack Overflow用户

发布于 2019-11-07 05:17:14

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

代码语言:javascript
复制
awk '/Solving for p/ && match($0,/=[^,]*/) && ++count==1{print substr($0,RSTART+2,RLENGTH-2)}' Input_file

解释:

代码语言:javascript
复制
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.
票数 0
EN

Stack Overflow用户

发布于 2019-11-07 05:57:19

我不确定,我是否理解你想要达到的目标。在这种情况下,您只想提取第一个匹配。

代码语言:javascript
复制
cat log | grep 'Solving for p' | head -1 | cut -d' ' -f9 | tr -d ','

根本不需要使用sed

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58742301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档