extract information from log file

Viewed 63

I have a log file which has below structure. I would like to get information after the "Initial residual = " text in each line and export it to a .txt file.

Time = 1996

smoothSolver:  Solving for Ux, Initial residual = 0.000674461, Final residual = 9.44251e-07, No Iterations 5
smoothSolver:  Solving for Uy, Initial residual = 0.00478922, Final residual = 7.06574e-07, No Iterations 7
GAMG:  Solving for p, Initial residual = 0.00899, Final residual = 9.67531e-07, No Iterations 10
time step continuity errors : sum local = 2.63988e-05, global = 4.03835e-06, cumulative = -1.29495e-05
smoothSolver:  Solving for epsilon, Initial residual = 0.00209075, Final residual = 3.67614e-07, No Iterations 6
smoothSolver:  Solving for k, Initial residual = 0.0017321, Final residual = 9.33393e-07, No Iterations 6
ExecutionTime = 27.9 s  ClockTime = 28 s

Time = 1997

smoothSolver:  Solving for Ux, Initial residual = 0.000659293, Final residual = 9.46747e-07, No Iterations 5
smoothSolver:  Solving for Uy, Initial residual = 0.00479541, Final residual = 7.03185e-07, No Iterations 7
GAMG:  Solving for p, Initial residual = 0.00975341, Final residual = 9.64034e-07, No Iterations 10
time step continuity errors : sum local = 2.6336e-05, global = 3.87822e-06, cumulative = -9.07129e-06
smoothSolver:  Solving for epsilon, Initial residual = 0.00213882, Final residual = 3.64484e-07, No Iterations 6
smoothSolver:  Solving for k, Initial residual = 0.00172913, Final residual = 9.36427e-07, No Iterations 6
ExecutionTime = 27.91 s  ClockTime = 28 s

The .txt file will have below structure

1996 0.000674461 0.00478922 0.00899 0.00209075 0.0017321
1997 0.000659293 0.00479541 0.00975 0.00213882 0.0017291   
.
.

With grep I can find the lines which contain a certain string. Then I can use cut to extract numbers at a certain column location and write it in a text file. For example, I am using the below command to extract information from lines that contain a specified string :

cat log | grep 'Solving for Ux' | cut -d' ' -f9 | tr -d ',' > resy.txt
cat log | grep 'Solving for Uy' | cut -d' ' -f9 | tr -d ',' > resy.txt

But I wanted to check if there is any other way in which I can extract all the information corresponding to two strings together. Thank you.

2 Answers

it's an ugly way, but can work for you:

#!/bin/bash
INPUT="/path/to/log.txt"
OUTPUT="/path/to/output.txt"
LINE=""
while IFS= read -r line
do
  echo "$line" | grep ^Time > /dev/null
  if [ $? -eq 0 ] ; then
    if [ -n "$LINE" ] ; then
      echo $LINE
    fi
    LINE=""
    LINE="${LINE}${line#*= }"
  else
    echo -e "$line" | grep "Initial residual" > /dev/null
    if [ $? -eq 0 ] ; then
      value=$(echo ${line} | sed -e 's/.*Initial residual = \(.*\), Final.*/\1/')
      LINE="${LINE} $value"
    fi
  fi
done < "$INPUT"
echo $LINE

Output:

1996 0.000674461 0.00478922 0.00899 0.00209075 0.0017321
1997 0.000659293 0.00479541 0.00975341 0.00213882 0.00172913

Using awk:

awk -F 'Initial residual = ' -v out=out.txt '
/^Time =/ {
sub("Time = ","")
i++
if (i>1){nl="\n"}
printf nl$0" " > out
}

/Initial residual/ {
sub(",.*","",$2)
printf $2" " > out
}

END {print "" > out}' log
Related