2017-04-15 57 views
1

我有一个文件,我想从中提取两个值(时间,C_F [6]),下面突出显示。它在CentOS 7环境中可以使用bash或gnuplot或r。我甚至不知道如何谷歌(例如,从文件bash提取值并不真正拿出解决方案)。它是否可行?从文件中提取值和平均值

我希望能够:

  1. 剧情时间与C_F [6]
  2. 平均C_F [6]

enter image description here

编辑1:

我认为这可能是上线,但它重现整个文件 个SED的/^.* 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 
+1

你能链接一个示例文件吗?我在这些情况下所做的是提取文件中的行(使用'readLine'等函数),然后查找包含特定模式(如Time ='和C_F [6])的行(使用'grep'或类似函数) '。也许可以清理这些行,以便数字部分将被保留。您可以复制粘贴文本从“// ****************”到任何一个例子。 – din

回答

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) 

正则表达式有更多优雅的方法,但我仍然在通过它找到我的方法。这应该是一个基本的方法。

+0

谢谢。这很好地工作。这可以从unix中的命令行运行吗? – HCAI

+0

我相信是的。尝试包括Rscript.exe二进制文件的路径,就像你为bash添加路径一样。虽然没有尝试过这一个。 – din

+0

测试了它并且工作正常,但明确地调用了'Rscript.ext nameofscript.R';另外,你可能想要玩弄你想要如何提取输出的情节(可能为情节创建一个设备)和文本输出的意思等。 – din