Search code examples
unixgreplogfiles

Grep two lines with unique keyword


I would like to take a txt file and do some sort of grep command and save the output to a file to be used in R. Consider I have the following from a log file:

current_t: 178.027

(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

rv: -8.421698E-06

(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

NullReferenceException: Object reference not set to an instance of an object
at ModelActions.Update () [0x00000] in <filename unknown>:0 

(Filename:  Line: -1)

 current_t: 179.7662

(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

rv: -8.413203E-06

(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

I want to be able to grab each line that has current_t and rv. Preferably I would I like to have it output as:

178.027  -8421698E-06
179.7662  -8.413203E-06

where all times are placed in a column and rv are placed in the following column. Is there a grep command for me to do something like this? Thanks!


Solution

  • awk to the rescue!

    $ awk -F: '$1~/current_t/{ct=$2} $1~/rv/{print ct, $2}' file          
    
     178.027  -8.421698E-06
     179.7662  -8.413203E-06
    

    assumes your data has always matching pairs.