# Number of line in website [closed]

I want to download the content of the website(contains text) and only a few lines from the content (from a specific number of the line up to the last line minus specific offset). Unfortunately, I do not know how to get the number of line in the content. For example, I want to replace 59 with the length of the content minus specific offset.

data1 = Import["https://en.wikipedia.org/wiki/Segment_tree"];
Snippet[data1, 51 ;; 59]


## closed as unclear what you're asking by corey979, MarcoB, Carl Lange, happy fish, bbgodfreyApr 28 at 15:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• You mean from line 51 to the end? Snippet[data1, 51 ;;]. – corey979 Apr 23 at 12:26
• @corey979 I edit the question to be more specific – Kiril Danilchenko Apr 23 at 12:33
• Snippet[data1, 51 ;; -17] – corey979 Apr 23 at 12:38
• @corey979 thank you for the answer,but if I'm interested to know the length of the content. How I can get it? – Kiril Danilchenko Apr 23 at 12:46
• So you want to know the length of the content, not take its snippet from a given line to the end, or to some numbered line from the end? So that's quite different than you have in the question: "replace 59 with the length of the content minus specific offset" would give the same outputs as my codes above. – corey979 Apr 23 at 14:07

One approach is to split the imported data on newlines using StringSplit so the length of the resulting list is the line count. Then we can use Riffle and StringJoin to get the selected lines:

Module[{start = 51, offset = 85, raw, data, linecount, stop},
raw = Import["https://en.wikipedia.org/wiki/Segment_tree"];
data = StringTrim@StringSplit[raw, "\n"];
linecount = Length@data;
stop = linecount - offset;
Print["# lines: ", linecount];
Print["  start: ", start];
Print["   stop: ", stop];
Print["# saved: ", stop - start];
data = Take[data, {start, stop}];
Riffle[data, " "] // StringJoin]


Alternatively, we could count newlines in the raw data and use Snippet as corey979 points out in the comments:

 Module[{start = 51, offset = 85, raw, linecount, stop, counter = 0},
raw = Import["https://en.wikipedia.org/wiki/Segment_tree"];
linecount = Block[{str = StringToStream[raw]},
While[Read[str, Record, NullRecords -> True] =!= EndOfFile,
counter++];
Close[str]; counter];
stop = linecount - offset;
Print["# lines: ", linecount];
Print["  start: ", start];
Print["   stop: ", stop];
Print["# saved: ", stop - start];
Snippet[raw, start ;; stop]]


The result for the website specified in the question gives:

This section describes the query operation of a segment tree in a \ one-dimensional space. A query for a segment tree, receives a point q \ x (should be one of the leaves of tree), and retrieves a list of all \ the segments stored which contain the point q x . Formally stated; \ given a node (subtree) v and a query point q x , the query can be \ done using the following algorithm: Report all the intervals in I ( v \ ). If v is not a leaf: If q x is in Int(left child of v ) then

length = ToExpression[ToString[StringCount[#,"\n"] & /@ FullForm[data1]]]+1
Snippet[data1,138;;length]