2017-04-15 4 views
1

私は以下の2つの値(Time、C_F [6])を抽出したいファイルがあります。そのCentOS 7環境では、bashやgnuplotやrを使うことができます。私はどのようにgoogleを(たとえば、ファイルbashからの値を抽出することは実際に解決策を思いつくわけではない)方法についてもわからない。それは可能ですか?ファイルからの値の抽出と平均

私ができるようにしたいと思います:[6]

  • 平均C_F [6]
  • enter image description here

    EDIT 1 C_F対

    1. プロット時間:

      これは行にある可能性がありますが、ファイル全体を再現します のsedの/^.* C_F [6] = //」C_F.pressure>

      EDIT 2 OUTPUTFILE:

      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

    私はそう信じています。 bashのパスを追加する方法と同じように、Rscript.exeバイナリのパスを含めてみてください。しかし、この1つを試していない。 – din

    +0

    それをテストし、うまくいきました。ただし、明示的に 'Rscript.ext nameofscript.R'を呼び出していました。また、プロットのような出力を抽出する方法(プロット用のデバイスを作成する)と平均などのテキスト出力をどのようにして再生したいのかを知りたいかもしれません。 – din

    関連する問題