2

Since I'm new in Julia I have sometimes obvious for you problems. This time I do not know how to read the certain piece of data from the file i.e.:

...
      stencil: half/bin/3d/newton
      bin: intel
Per MPI rank memory allocation (min/avg/max) = 12.41 | 12.5 | 12.6 Mbytes

Step TotEng PotEng Temp Press Pxx Pyy Pzz Density Lx Ly Lz c_Tr 

  200000    261360.25    261349.16    413.63193    2032.9855    -8486.073    4108.1669    
  200010    261360.45    261349.36    413.53903    22.925126   -29.762605    132.03134   
  200020    261360.25    261349.17    413.46495    20.373081   -30.088775     129.6742   


Loop

What I want is to read this file from third row after "Step" (the one which starts at 200010 which can be a different number - I have many files which stars at the same place but from different integer) until the program will reach the "Loop". Could you help me please? I'm stacked - I don't know how to combine the different options of julia to do it...

phipsgabler
  • 20,535
  • 4
  • 40
  • 60
kata248
  • 59
  • 4
  • I already know that I can use a "readuntil" command to reach "Loop" however I still do not know how to choose a third or second line after "Step". – kata248 Sep 16 '20 at 12:51

1 Answers1

2

Here is one solution. It uses eachline to iterate over the lines. The loop skips the header, and any empty lines, and breaks when the Loop line is found. The lines to keep are returned in a vector. You might have to modify the detection of the header and/or the end token depending on the exact file format you have.

julia> function f(file)
           result = String[]
           for line in eachline(file)
               if startswith(line, "Step") || isempty(line)
                   continue # skip the header and any empty lines
               elseif startswith(line, "Loop")
                   break # stop reading completely
               end
               
               push!(result, line)
           end
           return result
       end
f (generic function with 2 methods)

julia> f("file.txt")
3-element Array{String,1}:
 "200000 261360.25 261349.16 413.63193 2032.9855 -8486.073 4108.1669"
 "200010 261360.45 261349.36 413.53903 22.925126 -29.762605 132.03134"
 "200020 261360.25 261349.17 413.46495 20.373081 -30.088775 129.6742"
fredrikekre
  • 10,413
  • 1
  • 32
  • 47
  • Thank you for you answer. How I can start read from the line "200010" so 3rd line after "Step"? So I imagine that Im reading the file and when I see "Step" I start to read 3 lines later. – kata248 Sep 16 '20 at 12:58