473,395 Members | 1,694 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.

How do I remove lines of data above a keyword and below a keyword in a text file

First off, I would like to thank those who have given me help so far!

I would like to remove all the lines of data above the word Recipe in
a text file and all the lines below the words "End of Recipe" in this
file. I want to keep everything inbetween these two lines. Below is
a small sample of my text file and the code I have so far.

2,3,5,6,56,7,4,7,,,56,5,7,
-1,45,4,451,7,66,7,667,
"Recipe KX009.RCP, Date=02/11/07, Time=12:20:27,
2532,45,23,5,34,52345,,34,5234,345,45
3,34523452,45,3245,245,343443,344,45
,3445,4,45,,26,234,5,24,5145,5,65,56,5
"End of Recipe"
4,5,6,3,7,,8,6,8,68
,67,6,87,6,78,6,76
,243,7,76,9,7,9,6,,
Dim streamR As StreamReader = IO.File.OpenText("c:\MultiTemp1.txt")
Dim streamw As StreamWriter = IO.File.CreateText("c:
\filtered2.txt")
Dim strInput As String

While Not streamR.EndOfStream
strInput = streamR.ReadLine

If strInput.Contains("End") Then
Else
If InStr(strInput, "Recipe", CompareMethod.Text) >= 0
Then
strInput = streamR.ReadLine
streamw.WriteLine(strInput)
End If
End If
End While

streamw.Flush()
streamw.Close()

streamR.Close()

Thanks for your Help!

Apr 25 '07 #1
1 1651
I realize it's probably not a recognized way of doing things, but when I
have to extract text from a string where I know the start and end
identifier, I normally use the Split function. You can do it in two lines of
code:

Dim TempArray() As String = Split(OriginalString, "Recipe")
ExtractedString = TempArray(1).Substring(0, TempArray(1).Length - 8).Trim
TempArray(0) will contain the text before the first Recipe
TempArray(1) will contain the text between the two Recipe's
TempArray(2) will contain the text after the last Recipe

If both the starting and the ending identifier had been the same, you could
simply have used TempArray(1), but because of the "End of" and the quote
signs, a little more manipulation is needed. You remove the last 8
characters and trim the string (with your example, there will be a space in
the beginning of TempArray(1) ).

Good luck,
Johnny J.
Apr 25 '07 #2

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

Similar topics

0
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file)...
7
by: peraklo | last post by:
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...
2
by: Quentin | last post by:
I have a text file with lines that I would like to remove above a certain word and below another word. Example: 1231231231234123423434 2341234123412341242141 234512353764316236234...
0
by: Quentin | last post by:
First off, I would like to thank those who have given me help so far! I would like to remove all the lines of data above the word Recipe in a text file and all the lines below the words "End of...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
5
by: kanley | last post by:
I have a main table with a text description field. In this field, its populated with a string of data. I need to identify from this string of data the name of the vendor using some keywords. I...
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.