我有一个文件,我想从中提取下面突出显示的两个值(Time,C_F [6])。它在CentOS 7环境中,所以可以使用bash或gnuplot或r。我甚至不确定如何谷歌(例如从文件bash中提取值并没有真正提出解决方案)。可能吗?
我希望能够:
编辑1:
我认为这可能就行,但它会重现整个文件 sed's /^.* C_F [6] = //'C_F.pressure> OUTPUTFILE
编辑2:
Extract of the file:
/*---------------------------------------------------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 3.0.0 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
Build : 3.0.0-6abec57f5449
Exec : patchAverage p C_F -parallel
Date : Apr 15 2017
Time : 15:01:20
Host : "login2.jjj.uk"
PID : 59764
Case : /nobackup/jjjj/Silsoe/Solid/solid_0_LES/motorBikeLES
nProcs : 8
Slaves :
7
(
"login2.jjjj.59765"
"login2.jjjj.59766"
"login2.jjjj.59767"
"login2.jjjj.59768"
"login2.jjjj.59769"
"login2.jjjj.59770"
"login2.jjjj.59771"
)
Pstream initialized with:
floatTransfer : 0
nProcsSimpleSum : 0
commsType : nonBlocking
polling iterations : 0
sigFpe : Enabling floating point exception trapping (FOAM_SIGFPE).
fileModificationChecking : Monitoring run-time modified files using timeStampMaster
allowSystemOperations : Allowing user-supplied system call operations
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time
Create mesh for time = 0.18
Time = 0.18
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.3176
Time = 0.19
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.299
Time = 0.2
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.2704
Time = 0.21
Reading volScalarField p
Average of volScalarField over patch C_F[6] = -18.2349
答案 0 :(得分:1)
这是一种粗暴的做事方式:
# extract text from file line by line; will be indexed by line
sample <- readLines("D:\\tempFiles/example.txt")
# index the lines contaning "Time = "
timeI <- grep(x = sample, pattern = "Time = ")
# index the lines contaning "C_F[6]"; note that \\ is escape for [ and ]
C_FI <- grep(x = sample, pattern = "C_F\\[6\\]")
# extract lines and clean them
# note that these lines only contain "Time = values"; so just remove the "Time = "
timeval <-as.numeric(gsub(x = sample[timeI], pattern = "Time = ", replacement = ""))
# extract lines and clean them
# note that gsub removes all characters from te start (^) until "= "
C_FIval <- as.numeric(gsub(x = sample[C_FI], pattern = "^.*= ", ""))
# plot timve vs CF[6]
plot(y = timeval, x = C_FIval )
# get the mean
mean(C_FIval)
正则表达式还有更优雅的方法,但我仍然找到了解决这个问题的方法。这应该是一种基本方式。