Read every 5th line until the end of the text file - vb.net

Hi could someone please give me an example of how to read through a text file to the end and read every 5th line as a string? I know how to read a specific line in the text file using
line = System.IO.file.readAllLines(filepath)(linenum)
and also using streamreader to read each line etc..
But I want to go through the whole text file and pick out every certain number of lines. Pretty sure it's got something to do with a loop but I'm not too clued up yet.

As files are not line based, you would need to read all the lines and pick out the ones that you want.
You can use the Where method with the overload that gives you the index of the item, and the Mod operator to determine where every fifth line is:
Dim lines As String() = _
System.IO.File.ReadLines(filepath).Where(Function(line, i) i Mod 5 = 0).ToArray()
The number that you compare the expression to determines which lines you get. i Mod 5 = 0 starts at the first line and then every fifth from there, while i Mod 5 = 4 starts at the fifth line and then every fifth from there.
(The ReadLines method is better than the ReadAllLines for this, as it doesn't read all the lines into memory first, but returns an enumerator so that you can process the lines as they are read.)

Related

Read and split line by line in text file

I am trying to read a text file from my applications resources. For each line in this text file I want to split the text before and after the comma.
Each line in txt file looks like this:
-125.325235,4845636
My issue is that the function loops and does not end constantly repeating the for each statement
For Each Line As String In My.Resources.CompanyBases
MsgBox(My.Resources.CompanyBases.Split(","c).First)
MsgBox(My.Resources.CompanyBases.Split(","c).Last)
Next
Firstly, don't ever get a resource over and over like that. Those properties are not "live". Every time you get the property, the resource has to be extracted from your assembly. If you need to use the value multiple times, get the property once and assign it to a variable, then use that variable over and over.
Secondly, you're not getting a file. The whole point of resources is that they are not distinct files but rather data compiled into your assembly. It's just a String like any other. How would you usually split a String on line breaks?
Finally, you have a For Each loop with a loop control variable Line, yet you never use that variable inside the loop. It should be Line that you're splitting inside the loop, not the resource property containing all the lines.
For Each line In My.Resources.CompanyBases.Split({Environment.NewLine}, StringSplitOptions.None)
Dim fields = line.Split(","c)
Debug.WriteLine(fields(0))
Debug.WriteLine(fields(1))
Next
Note that, if you're using .NET Core, Split will accept a String as well as a String array.

Apache Jmeter CSV Data Set Config Element only read the first line of the csv

I have a CSV file and I want to add the values to a variables. I write the variables in the "Variable Name" separated with commas, like: "a,b,c,d,e,f,g,h",
In the CSV file I have 5 words in the first line and in the Debug Sampler I get the following.
a=word one, b=word two, c= word three, d= word four, e=word five,
and I have no f,g,h variables.
How could It go to the next line?
Thank you for your help. :)
Please increase the Number of Threads(users) count to 2 to go in next line, This field can be found in thread group settings.
i.e
Increasing the thread count will execute again and in second run it will pick next line

Vb.net What is the best way to parse a txt file that gets updated

what I need to accomplish: I need to read a text file that gets updated every 10 sec and dump the new data into. I need to find the best way to do this. I've tried streamreader and file.readalllines, I can get the contents of the txt file, but I don't know how to compare and dump the added lines. Any input would be appreciated, thank you.
Under the assumptions that this is a log file you're reading, where the data is being appended to end, and you're reading the file every 10 seconds, why don't you keep track of the number of lines read or the last line number from the last read?
You can then check to see whether the current read has exceeded the previous line number or number of lines read. If it has exceeded, you should be able to work out what the new lines were added because you know where you read until previously. You can read from the (previous read line + 1) until the end of file.

Pick a random line from a text file and store it in a variable (Python 3)

I am trying to code a program which reads a file, which will contain many words (one word per line), then selects a random line (word) from the file, so I am able to store it in a variable for me to use later on.
I don't really know where to start as I am not very experienced. Any help would be appreciated.
Well first you will need to open the file
file = open('filename.txt', 'w')
Then you need to read the file you can read each line into a list by doing words = file.readlines (this can also be done with a loop or in a number of other ways)
Then you can use the random module to generate a random number and get the word from that index in the words list. Then just store that word to a variable.
There are other ways of doing this but this is one of the easiest.

VB notepad spliter

So I just started a new program and in this program I need a feature that will basically take a text file or just a richtextbox, and then equally split it. So lets say I have a file with 200 lines of text. Then I want to split it up into 10 files of 20 lines. Please help I need some type of direction! -thanks
Count the textbox length by (textbox1.text.length)i.e.=200 character and divide it by 2 = 100 then make a loop which goes to 200 then keep condition in loop body if it equal to 100 put it to string variable and write it to file and so on and so forth...
First of all create a string array
Dim S() As String = TextBox1.Text.Split(vbNewLine)
this will allow you to put every line in an item of the array named S
to get the lines count use
S.Length
OR
S.Count()
now you got the lines and the lines' count all you have to do to divide them and go from there...