By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

copy one column from text file to another text file

P: 4
Dear All,

I am a Geophysicist by profession and learning Vb .net online. I am trying to read a large text file with three columns as following:
Expand|Select|Wrap|Line Numbers
  1.  IL19623 research_Project 2912394
  2.  IL19625 research_Project 2912425
  3.  IL19627 research_Project 2912425
  4.  IL19629 research_Project 2912450
  5.  IL19631 research_Project 2912450
  6.  IL19633 research_Project 2912477
  7.  IL19635 research_Project 2912477
  8.  IL19637 research_Project 2912504
  9.  IL19639 research_Project 2912504
  10.  IL19641 research_Project 2912521
  11.  IL19643 research_Project 2912521
  12.  IL19645 research_Project 2912542
  13.  IL19647 research_Project 2912542
Each column is seperated from other with different spacing.
I want to copy the first column to a new text file and i want to Sum the third column to get a total. Thanks in advance for the help..
I will highly appreciate if you could tell some useful commands to make mathematical calculations from one textfile to another.
thanks and Best regards
Syed Emad
Apr 6 '15 #1
Share this Question
Share on Google+
5 Replies


P: 3
Well, as 1st step you could try to split each row in a string array
Expand|Select|Wrap|Line Numbers
  1. ' *** Read all textfile and put it in a string ****
  2. dim all_db as string = IO.File.ReadAllText(file_name)
  3. dim splitted_string() as string = split(all_db, vbLf & vbCr)
  4. ' *** It assume that you have LineFeed + CR for each line 
  5. dim N as integer, Sum1 as integer = 0
  6. For N = 0 to splitted_string.Lenght -1
  7.  ' *** Split again the single item in the array
  8. dim sub_item() as string = split(splitted_string," ")
  9. ' *** Now you have another sub-array which contains :
  10. ' sub_item(0) = "IL" + your number #1
  11. ' Lets cut the "IL" string
  12. sub_item(0) = sub_item(0).replace("IL","")
  13. ' sub_item(1) "research....etc etc"
  14. ' sub_item(2) = your number #2
  15. ' Now you have to add as you want, for eg.
  16.  Sum1 += val(sub_item(0)) + Val(sub_item(2))
  17. next
  18. '************ Now you have in Sum1 the total *******
Of course it can be improved, eg. with Try...Catch construct
and /or write the summ or partial of array in another file.
I hope it will help you....
Apr 6 '15 #2

P: 4
Thanks for your instant reply ...
i actually tried the code, but facing one error in the following line:

Expand|Select|Wrap|Line Numbers
  1.  Dim sub_item() As String = Split(splitted_string, " ")
Error= Value of type '1-dimensional array of String' cannot be converted to 'Sting'.

secondly, I am copying the text again as below by defining the start and end of the columns as they are shown very close to each other when i copy paste the text here. The columns are unevenly spaced.
col 1 = 1-7 characters
col 2 = 9-24 characters
col 3 = 26-32 characters


Expand|Select|Wrap|Line Numbers
  1.  IL19623 research_Project 2912394
  2.  IL19625 research_Project 2912425
  3.  IL19627 research_Project 2912425
  4.  IL19629 research_Project 2912450
  5.  IL19631 research_Project 2912450
  6.  IL19633 research_Project 2912477
  7.  IL19635 research_Project 2912477
  8.  IL19637 research_Project 2912504
  9.  IL19639 research_Project 2912504
  10.  IL19641 research_Project 2912521
  11.  IL19643 research_Project 2912521
  12.  IL19645 research_Project 2912542
  13.  IL19647 research_Project 2912542
Thanks !!
Apr 6 '15 #3

P: 3
You are right. it was my mistake :
Expand|Select|Wrap|Line Numbers
  1. Dim sub_item() As String = Split(splitted_string(N), " ")
But it seems that every substring doesnt have a space, so it will retrieve same string
so u can try with
Expand|Select|Wrap|Line Numbers
  1. str1 = splitted_string(f).substr(2,5) ' the number of 5 digits
  2. str2 = splitted_string(f).substr(26,7)
then u can convert the string to int

try with that above and let us know
Apr 6 '15 #4

P: 4
hi genex1000

Thanks a lot for the correction. I ran the code, but its only reading first line and only IL19623 .. it is not reading the whole column to the end.
the split is correct as i need to read these characters, but it not going down to the end of the textfile ..
Apr 7 '15 #5

P: 4
can anyone help to get the solution for the above mentioned problem of copying one complete column in a textfile to another textfile ????
Apr 14 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.