1
$\begingroup$

I have a txt file which contains a lot of data (in fact it's the result file of a .nb file which I have run on a cluster). I want to use the data to plot some graphs in a new .nb file.

Since there are a lot of data in the txt file, it's a really time-consuming task to copy and paste each desired part to the new .nb file, so I decided to put two markers at the start and at the end of each part of data by printing a special word. For example in the original .nb file, I write Print["start of density data"] command at the start of density data part, then I print my density function (which shows the data for density as a list), after this I write Print["end of density data"]. By doing so, I have determined the start and end of my desired part, then I need to write a code which finds the line number of "start of density data"(for example it's 600) and also the line number of "end of density data"(for example it's 700).

Now I can write a code to gives me lines 601-699 which contains my desire data. The latter is a straightforward task, but my main problem is how to write a code which finds the line numbers of two markers, I tried Read and Find after importing my txt file but they don't give line numbers. Any idea?

I have attached a dummy file here to facilitate answering.

$\endgroup$
8
  • $\begingroup$ Seems like the easiest solution would be to write the sections of data to separate files. Since you are able to print start/end, this should be easy. Following the link to the dummy file results in "The file you requested has been deleted". $\endgroup$ Commented May 6, 2021 at 15:55
  • $\begingroup$ What do you mean by "write the sections of data to separate files"? Please explain more $\endgroup$
    – Wisdom
    Commented May 6, 2021 at 16:03
  • $\begingroup$ In your original .nb file you print "start" then you print output from densityFunction and then you print "end". Instead of printing the densityFunction, save its output to a file. Put[densityFunction[arguments], "some file name"]. To read it later densityData = Get["some file name"] $\endgroup$ Commented May 6, 2021 at 16:32
  • $\begingroup$ Good idea, but I have to save output of 15 functions in each nb file, and I have 63 such nb files, so I need 945 separate files which are soooo much to handle! $\endgroup$
    – Wisdom
    Commented May 6, 2021 at 16:38
  • $\begingroup$ Can you provide a sample file? As I mentioned earlier, the link you provided no longer has the sample. $\endgroup$ Commented May 6, 2021 at 16:50

2 Answers 2

2
$\begingroup$

Here is one way to do it. The Lines option in Import will cause the import of lines as elements of a Table object.

file = Import["/somePath/data.txt", "Lines"];
Position[file, "\"start of density data\""][[1, 1]]
Position[file, "\"end of density data\""][[1, 1]]
(*
46
48
*)
$\endgroup$
9
  • $\begingroup$ Thanks a lot. What does "Lines" do in import command? $\endgroup$
    – Wisdom
    Commented May 6, 2021 at 18:18
  • $\begingroup$ @Wisdom It will cause the import of lines as elements of a Table object. $\endgroup$
    – A.G.
    Commented May 6, 2021 at 18:21
  • $\begingroup$ I wonder how your code works by [[1,1]] while file is not a list. Also when I use this code just for fvar without quotation I get the error "Part 1 of {} does not exist", what is the logic of your code? $\endgroup$
    – Wisdom
    Commented May 7, 2021 at 5:42
  • $\begingroup$ I wonder how your code works by [[1,1]] while file is not a list. Also when I use this code just for fvar without double quotation I get the error "Part 1 of {} does not exist", what is the logic of your code? How can I use it for finding line number of any string in the imported file? $\endgroup$
    – Wisdom
    Commented May 7, 2021 at 5:48
  • $\begingroup$ In my test the command Position[file, "\"start of density data\""] returns {{46}}, thus the need for [[1,1]]. If what you want is the number of a line that contains some word/string you may have to replace "\"start of density data\"" by some pattern. $\endgroup$
    – A.G.
    Commented May 7, 2021 at 6:24
1
$\begingroup$
text = Import["~/Downloads/data.txt", "Lines"];

densityPosition = Position[text, "\"start of density data\"" | "\"end of density data\""] + {1, -1}

densityData = Extract[text, densityPosition] // ToExpression
$\endgroup$
6
  • $\begingroup$ Thanks but I want to find the position of fvar $\endgroup$
    – Wisdom
    Commented May 7, 2021 at 16:55
  • $\begingroup$ In data.txt, fvar is after "end of density data". So are you asking a different question? $\endgroup$ Commented May 7, 2021 at 17:22
  • $\begingroup$ fvar was just a example. Generally I want a code to find any string in an imported file $\endgroup$
    – Wisdom
    Commented May 7, 2021 at 17:25
  • 1
    $\begingroup$ Depending on the structure of the imported data (single string or list of strings or ...) you would have to use Position or StringPosition. $\endgroup$ Commented May 7, 2021 at 17:30
  • 1
    $\begingroup$ @A.G. For simple patterns RegularExpression is not needed. StringMatchQ accepts * (zero or more characters) and @ (one or more characters excluding uppercase letters). Check the 'Details and Options' section in the documentation. $\endgroup$ Commented May 8, 2021 at 1:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.