473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Counting the lines of text, but upwards...

Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...

Mar 1 '07 #1
7 2072
pe*****@gmail.com wrote:
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...
You can't jump around in a text stream, so you would have to open it as
a binary stream. You can read a block of data into a buffer, but you
have to locate the line breaks yourself so that you can decode the bytes
between them into strings.

--
Göran Andersson
_____
http://www.guffa.com
Mar 1 '07 #2
Or just read in the whole file, take off the last 27 lines, then write it
back out. Here's an untested version.

Dim lines() as String = File.ReadAllText("c:\data.txt").Split(crlfs,
StringSplitOptions.None)
Dim numOfLines = lines.Length

Then use WriteAllText to output the data.

Dim newText as String = String.Join(ControlChars.CrLf, lines, 0,
lines.length - 27)
File.WriteAllText("c:\output.txt",newText,False)

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
---------------------------------------------------------
"Göran Andersson" <gu***@guffa.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
pe*****@gmail.com wrote:
>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...

You can't jump around in a text stream, so you would have to open it as a
binary stream. You can read a block of data into a buffer, but you have
to locate the line breaks yourself so that you can decode the bytes
between them into strings.

--
Göran Andersson
_____
http://www.guffa.com

Mar 1 '07 #3
Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out to
the new file.

<pe*****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...

Mar 2 '07 #4
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc*************@enterprocity.comwrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...


Mar 2 '07 #5
If the original author is willing to do a little labor I'd suggest starting
by reading this thread:

http://groups.google.com/group/micro...7beee5cee8da15

At the end of it I supplied a class named Copier to produce the results
another guy wanted (never heard from him.) But clearly it is a simple
matter to use this class as a starting point adding a method (named Splitter
perhaps) which takes three parameters. Two strings representing the two
parts of the file and a numeric representing the number of lines desired in
the first file.

The Copy method I supplied will copy a fixed number of lines to a file so
the only required adjustment is to open a second file (the parameter
contains the name) and to copy the remaining lines to it. At the end you
have the original file intact along with two new files split at the line
number specified. If it is necessary to specify the number of lines to be
placed in the second file (rather than the first) then simply add another
method that returns the number of lines in the original file and do the math
to calculate how many lines should be written to the first file.

And it appears that I failed to call Dispose() on the FileStreams in that
code so one might want to add that not that it wouldn't work without it.
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:PY******************************@comcast.com. ..
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc*************@enterprocity.comwrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
>Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegr oups.com...
>>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...



Mar 2 '07 #6
Yeah, I did not read that he also wants to save the lines that are not the
last 27 to another file. In this case I would write a custom FIFO stack that
writes the lines that get pushed off the bottom to a file (so you get the
ones - the 27 that remain in the stack) and then write the ones left in the
stack to another file :)
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:PY******************************@comcast.com. ..
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc*************@enterprocity.comwrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
>Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegr oups.com...
>>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation...

Thanks in advance...



Mar 3 '07 #7
pe*****@gmail.com wrote:
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:
I provided a reply in March 2006 to another person with a similar
request, and while I didn't intend to write your entire solution, I have
done just that below (watch for line-wrapping) -

Dim blPossibleEOL As Boolean = False
Dim byTemp As Byte = 0
Dim iFileNum As UInt16 = FreeFile()
Dim sInputFileName As String = "YOUR INPUT FILENAME HERE!!"
Dim sTextLines As String = ""
Dim iCounter As UInt16 = 0

Dim iFileEnd As Integer = FileLen(sInputFileName) - 2
Dim iFileSeekPosition As Integer = iFileEnd
FileOpen(iFileNum, sInputFileName, OpenMode.Binary, OpenAccess.Read)

Do
FileGet(iFileNum, byTemp, iFileSeekPosition)
If blPossibleEOL Then
If byTemp = &HD Then
iCounter += 1
If iCounter = 27 Then 'Change this for whatever number of lines
you are wanting.
Exit Do
Else
blPossibleEOL = False
End If
Else
blPossibleEOL = False
End If
ElseIf byTemp = &HA Then
blPossibleEOL = True
Else
blPossibleEOL = False
End If
iFileSeekPosition -= 1
Loop

sTextLines = StrDup(iFileEnd - (iFileSeekPosition + 1), " ")
FileGet(iFileNum, sTextLines, iFileSeekPosition + 2)
FileClose(iFileNum)

My.Computer.FileSystem.WriteAllText("CUT-LINES OUTPUT FILENAME HERE!!",
sTextLines, False)

Dim srInput As New IO.StreamReader(sInputFileName)
Dim srOutput As New IO.StreamWriter("TRIMMED-LINES OUTPUT FILENAME HERE!!")

iFileEnd = iFileSeekPosition
iFileSeekPosition = 0
Do
sTextLines = srInput.ReadLine
srOutput.WriteLine(sTextLines)
iFileSeekPosition += (Len(sTextLines) + 2)
Loop Until iFileSeekPosition >= iFileEnd

srInput.Close()
srOutput.Close()

'Kill(sInputFileName)
I have tested this with a 200,000 line Text File and the files were
created almost instantly!

Basically, it works like this -

The first loop opens your Input File (Binary Read Mode), and starting
from the end of the file, counts the number of lines you are wanting to
cut. (iCounter = 27 in this case). It does this by counting the CrLf
characters in your text file. This approach has a tremendous speed
advantage as it doesn't have to count ALL of your Input File lines from
the beginning.

Once it has reverse-counted the number of lines you are wanting, it
notes the current file (byte) position, reads the lines from that
position until the end of the file into a string and writes-out those
lines to your new file. You now have a file with just the number of
lines that you required to be cut.

It then opens your Input File in a StreamReader and starts reading line
by line in the second loop. As each line is read, it is written to
another file, which will eventually contain everything except the last
few lines you want cut. It knows when to stop writing by matching the
number of characters read so far (allowing extra 2 bytes for CrLf on
each line) to the original Seek Position obtained earlier.

As already mentioned, this routine is VERY fast, not necessarily pretty,
but fast!

I would encourage you to add error-handling etc., but I trust this gives
you a strong foundation to work from.
ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Derek Basch | last post by:
Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets Thanks, Derek Basch
5
by: Anders K. Jacobsen [DK] | last post by:
Hi We have a rather large asp.net project with serveral utility projects (written in C#). Is there at tool out there which can give an estimate of the total amount of code lines all projects...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
3
by: Brent | last post by:
Say that I have a text box that holds 5 lines of text. If the user enters ten text lines, I want the first four lines of text displayed, followed by a "More" hyperlink. The link will pop up another...
5
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
4
by: anoehre | last post by:
Hi! I have a multi-line text box in a compact framework c# application. I need a method to calculate the excact number of lines of the textbox and a method to calculate the number of used lines...
10
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
7
by: Mark..... | last post by:
Hi, Can someone tell me the easiest way to count the number of lines in a text file? I can write a loop to do this but it seems cumbersome.... there must be an easier way?? Thanks in...
6
by: =?Utf-8?B?SGFucyAtIERpYUdyYXBoSVQgLQ==?= | last post by:
Hi! sorry for a dumb question. I have a textbox where multiline=true and I want to set the scrolbar from none to vertical when the textbox have more then f.ex. 5 lines, or reverse. How can...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.