1

I have a textfile including thousends of lines and each line have 899 values/columns separated by ';' (but i can change the seperation if needed)

Is there a way to read a defined column (i want to tell the code which (per read icolumn)) without reading the whole line into variables?

So what I know: read(unit,*) a,b,c,d,e,... (899 times is not cool) otherwise I would put all this values in an array. And after I could read my column with: array(i,icolumn)

I would like a direct way.

Tom Lucas
  • 13
  • 2
  • Do you know on which column (as in characters) is the value you want to read? Generally the answer will be read your line into a character string and parse it but in special cases you might be lucky. You do NOT have to parse all the vales and store them into an array. – Vladimir F Героям слава May 02 '19 at 15:15
  • I'm not sure if i understand your question. my line looks like: 2.75420e+002;2.75327e+002;2.75281e+002;2.75178e+002;2.75052e+002;2.74908e+002;2.74847e+002;2.74761e+002;2.74689e+002;2.74497e+002;.... and so on. so there is a fix number of characters. And yes I want a specific column eg 165. So my program should ask me which column i want. then I write 165. Then he reads only column 165 to a new file – Tom Lucas May 02 '19 at 15:28
  • Though not Fortran, I guess some command (if available) may be more convenient...(like "cut -f 165 -d \; matrix.dat") – roygvib May 02 '19 at 16:06

2 Answers2

1

If you know the number is at character 165 then you just use the t or tr or x descriptor to move there and read the number

read(unit,'(t165,f12.0)' array(whatever)

if you need to put the number 165 into the format string you can use this function.

  • ok that could work, if I change something. Is it possible to use a variable for that? I mean: my program asks me which cell, then I type 165 into the variableX and then the program uses: read(unit,'(t"variableX",f12.0)' array(whatever) – Tom Lucas May 05 '19 at 16:12
  • @TomLucas Did you read the end of the sentence including the link? But in my code the number after `t` is NOT the number of the csv cell. It is the text file column! – Vladimir F Героям слава May 05 '19 at 16:16
  • Yeah I red the link, but don't understand it very well. (165 is just an example. If I want the 165th cell I multiply it with 13(the number of digits) thats no problem :) – Tom Lucas May 05 '19 at 16:32
  • `read(unit,'(t'//itoa(variableX)//',f12.0)' array(whatever)` but you can use any other of those methods in the link. It is basically this https://stackoverflow.com/questions/9881186/format-string-for-output-dependent-on-a-variable – Vladimir F Героям слава May 05 '19 at 16:37
  • ahhh your `itoa` changes the integer to a character? Ok I was a bit confused, sry... – Tom Lucas May 05 '19 at 17:43
0

My datafile testdata.dat looks like:

2.75420e+002;2.75327e+002;2.75281e+002;2.75178e+002;2.75052e+002;2.74908e+002;2.74847e+002;2.74761e+002;2.74689e+002;2.74497e+002
2.75420e+002;2.75327e+002;2.75281e+002;3.75178e+002;2.75052e+002;2.74908e+002;2.74847e+002;2.74761e+002;2.74689e+002;2.74497e+002
2.75420e+002;2.75327e+002;2.75281e+002;4.75178e+002;2.75052e+002;2.74908e+002;2.74847e+002;2.74761e+002;2.74689e+002;2.74497e+002

That is, 10 values per line and 3 lines, and note how I've changed the fourth column values from your original data.

My program for reading it asks the user for the column to read:

program readspeccol

integer :: readcol, i,j
double precision :: a(3)   ! 3 = number of lines in file
character(len=1) :: junk


print *,'input column:'
read(5,*) readcol

open(unit=15, file='testdata.dat')

do i = 1, 3
  read(15,'(10(es12.5,1a))') (a(i),junk, j=1,readcol)
  ! 10 = number of values in line
enddo


print *, a


end program readspeccol

If I tell it to read the fourth column, execution is:

$>  ./a.out
 input column:
4
   275.17800000000000        375.17800000000000        475.17800000000000  

This program could be tweaked further, but I think it is basically what you are looking for.

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • It really isn't necessary to read all the junk. The poor CPU has to do loads of conversion from strings to numbers for nothing. Besides, specifying nonzero d in esw.d can lead to unexpected results when the number on input does not contain a decimal point. – Vladimir F Героям слава May 02 '19 at 20:06