extract certain values from file with re

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Oct 6 12:37:58 EDT 2006


Fabian Braennstroem:
> A more difficult log file looks like:
> ...
> With my sed/awk/grep/gnuplot script I would extract the
> values in the 'U-Mom' row using grep and print a certain
> column (e.g. 'Max Res') to a file and print it with gnuplot.
> Maybe I have to remove those '|' using sed before...

This is possible (quite raw) solution for the second log using string
methods only:

data = """
======================================================================
 OUTER LOOP ITERATION =    1                     CPU SECONDS = 2.40E+01
 ----------------------------------------------------------------------
 |       Equation       | Rate | RMS Res | Max Res |  Linear Solution |
 +----------------------+------+---------+---------+------------------+
 | U-Mom                | 0.00 | 1.0E-02 | 5.0E-01 |       4.9E-03  OK|
 | V-Mom                | 0.00 | 2.4E-14 | 5.6E-13 |       3.8E+09  ok|
 | W-Mom                | 0.00 | 2.5E-14 | 8.2E-13 |       8.3E+09  ok|
 | P-Mass               | 0.00 | 1.1E-02 | 3.4E-01 |  8.9  2.7E-02  OK|
 +----------------------+------+---------+---------+------------------+
 | K-TurbKE             | 0.00 | 1.8E+00 | 1.8E+00 |  5.8  2.2E-08  OK|
 | E-Diss.K             | 0.00 | 1.9E+00 | 2.0E+00 | 12.4  2.2E-08  OK|
 +----------------------+------+---------+---------+------------------+

 ======================================================================
 OUTER LOOP ITERATION =    2                     CPU SECONDS = 8.57E+01
 ----------------------------------------------------------------------
 |       Equation       | Rate | RMS Res | Max Res |  Linear Solution |
 +----------------------+------+---------+---------+------------------+
 | U-Mom                | 1.44 | 1.5E-02 | 5.3E-01 |       9.6E-03  OK|
 | V-Mom                |99.99 | 1.1E-03 | 6.2E-02 |       5.7E-02  OK|
 | W-Mom                |99.99 | 1.9E-03 | 6.0E-02 |       5.9E-02  OK|
 | P-Mass               | 0.27 | 3.0E-03 | 2.0E-01 |  8.9  7.9E-02  OK|
 +----------------------+------+---------+---------+------------------+
 | K-TurbKE             | 0.03 | 5.4E-02 | 4.4E-01 |  5.8  2.9E-08  OK|
 | E-Diss.K             | 0.05 | 8.9E-02 | 9.3E-01 | 12.4  2.6E-08  OK|
 +----------------------+------+---------+---------+------------------+
 """.splitlines()

print [float(l.split("|")[4]) for l in data if 'U-Mom' in l]

Output:
[0.5, 0.53000000000000003]

Bye,
bearophile




More information about the Python-list mailing list