Search code examples
pythonmatplotlibawkgnuplot

How to obtain a specific text from a file?


I generated a data file with the following format:

0.1
Analytic value = 340.347685734
Approximated value = 332.45634555
--
0.2
Analytic value = 340.936745872
Approximated value = 332.57893789
--
0.3
... and so on

I want to plot the analytic and approximate values in matplotlib/gnuplot against the input parameter (0.1, 0.2, etc). Usually, before generating the data file, I use to generate them with an awk script that puts the three values in three columns which is very easy to plot. However, here I accidentally generated the data file in a different format. How can I convert this text file to the following (maybe using regex or awk!):

0.1 340.347685734 332.45634555 
0.2 340.936745872 332.57893789
0.3 ... and so on

Or is there a way that I can plot the data without converting the format using gnuplot/matplotlib?

EDIT: I have attempted to do it using python3. The following is my code:

file = open("myFile.dat",'r')
newFile = open("newFile.dat", 'a')
for i in range(4000):
  col1 = file.readline().split[-1]
  col2 = file.readline().split[-1]
  col3 = file.readline().split[-1]
  _ = file.readline().split[-1]
  line = col1 + " " + col2 + " " + col3
  newFile.write(line)

However, I was getting some error TypeError: 'builtin_function_or_method' object is not subscriptable which I didn't understand and I think this is a very inefficient code. That's why I asked in the SE. All the solutions presented so far work quite well. I marked the solution with awk as the accepted answer because it's simple and elegant. Also, I appreciate the solution that uses gnuplot only which also uncover a side of gnuplot for me.


Solution

  • I would harness GNU AWK for this task following way, let file.txt content be

    0.1
    Analytic value = 340.347685734
    Approximated value = 332.45634555
    --
    0.2
    Analytic value = 340.936745872
    Approximated value = 332.57893789
    --
    

    then

    awk '/^--$/{print "";next}{printf "%s ",$NF}' file.txt
    

    doess output

    0.1 340.347685734 332.45634555 
    0.2 340.936745872 332.57893789
    

    Explanation: for line being -- just print newline and go to next one, for all others lines do output last field followed by space and not newline. If you want to know more about NF then read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

    (tested in GNU Awk 5.1.0)