2017-04-15 56 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