How to read/write a specific value from a .txt file

Dear All,

I need your help on reading and extracting a specific value from a .txt file which does not a have a specific format. Let’s say that the .txt is that snapshotted below and we want to extract the highlighted value. Thanks in advance!

1 Like

An attempt to reformulate your question: Do you need to read out a line/a special value from this line where 5.263 highlighted by you may serve as an anchor?

If this were the case, based on the screen photo shared, it looks like you have tabulated data, where columns are separated by explicit spaces or tabulators. One platform agnostic approach would be to reach out for AWK.

As an illustration, let’s assume you the following file example.txt:

 4  14  15   1
 9   7   6  12
 5  11  10   8
16   2   3  13

and you are interested in the second value in line 3 (11). You could anchor AWK’s action to yield this value only if the value of the fourth element in this line equates to 8, i.e. on the CLI by the command

awk '$4 == 8 {print $2}' example.txt

As you note, the count of columns starts by one. If your report needs the whole line, issue

awk '$4 == 8 {print}' example.txt

and if you need the values of the fourth and second column (in this sequence), issue

awk '$4 == 8 {print $4, $2}' example.txt

If you have an hour or two, have a look at Benjamin Porter’s gentle introduction Awk: Hack the planet['s text]! consisting of a video presentation, and commented set of challenges. He mirrored test data and test scripts on GitHub.

Your question is impossible to answer until you articulate the algorithm by which you plopped the splotch of yellow in the image. What if your marker tip had landed on a blank space in the table?

Is a specific location in the table (Table 2, column 5, row 5) the basis for selection, or the value itself (largest number in the table that is less than 6.0) or that it satisfies some relation (the largest number in the second table that is equal to the sum of three distinct numbers in the first table), or that it matches some information that is not present in the tables, such as the current inflation rate in Norway?

Maybe GitHub - jacobwilliams/fortran-csv-module: Read and Write CSV Files Using Modern Fortran (@jacobwilliams)
as space separated columns are CSV format with spaces as delimiters.