473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

copy one column from text file to another text file

4 New Member
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
5 1836
genex1000
3 New Member
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
semad
4 New Member
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
genex1000
3 New Member
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
semad
4 New Member
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
semad
4 New Member
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

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

Similar topics

5
by: cwbp15 | last post by:
Using Visual Studio C# When I ran the following code: System.IO; private void Button1_Click(object sender, System.EventArgs e) { //FileStream fs = File.Create(Server.MapPath("test.txt"));...
3
by: C# to XML | last post by:
Hi Friends, I am new to csharp , I am using the following code to write into xml file : Just go through the code : fp = File.OpenText(Server.MapPath(".\\upload\\") + "test.txt"); string...
4
by: steele20 | last post by:
I have a problem, Im reading numbers from a text file like this 10 20 3 x1 y1 x2 y2 and so on, x and y are values and theres no limit but will probably not be more than 5 or so.
5
by: Simon Rigby | last post by:
Hi folks, Apologies if this is not directly relevant but I was struggling to find a group with the appropriate context. So as my problem is in an aspx web site I thought I'd try here first....
8
by: pappu | last post by:
Hello friends, I want to convert .xls file into text file.So can anybody pls help me out. Thxxxxxx, Sachin.
1
by: vjunkl | last post by:
Hello, I have a content text file in my project and I'd like to copy it to a folder at some point in the execution of the application(not during a build). If this is not possible, how about...
7
by: ahmed222too | last post by:
how can i copy a text file and paste it in a specific path by VB6 code another question how can i create afolder in a specific path by VB6 thank you
2
by: ahmed222too | last post by:
when i use this code to copy a text file and paste it in a specific path: Dim fso As New FileSystemObject fso.CopyFile "C:\Files\file.txt", "C:\Files1\file_copy.txt" this error appear ...
2
by: progvar | last post by:
Hi! can any one help me by providing the method when i open any text file and convert into pdf format. I searched on the net and i got some code but i am not understanding this code and it also...
1
by: rajeev4983 | last post by:
I have an EDI file whose structure is given below. This file has multiple records, each record contains a header(e.g EDI.DD.0000000001.20130809), then contents (i.e multiple paragraphs of text) and...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.