473,406 Members | 2,387 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,406 software developers and data experts.

Any idea how to do this?

Hi guys! Got stuck with this..
I have a chunks of text separated with blanc line. All I need is to
take every last 3 lines of that text before the blanc line and put it
in to the new text file: example:

1some text
2some text
3some text
4some text
5some text

6some text
7some text
8some text
9some text
10some text

11some text
12some text
13some text
14some text
15some text

End result mast be...

3some text
4some text
5some text
8some text
9some text
10some text
13some text
14some text
15some text

Any idea?

Sep 5 '06 #1
3 1059
kulet wrote:
Hi guys! Got stuck with this..
I have a chunks of text separated with blanc line. All I need is to
take every last 3 lines of that text before the blanc line and put it
in to the new text file: example:

1some text
2some text
3some text
4some text
5some text

6some text
7some text
8some text
9some text
10some text

11some text
12some text
13some text
14some text
15some text

End result mast be...

3some text
4some text
5some text
8some text
9some text
10some text
13some text
14some text
15some text

Any idea?
Hi,

That is easy. You must replace all double newlines with 1 newline.
But beware: you say 'blanc line', but you must first find out how that is
coded in your file.

Often a newline is \n but sometimes it is \n\f or something like that.
(\f for linefeed)
It is also implemented differently on Unix, Windows, and Mac, just to make
your life misserable.

So you need something like this:
$newLineOnMySystem = "\n";
$yourString = "sometextsometextetcetc";

// Now replace all double occurences with single ones.
$yourString = str_replace($newLineOnMySystem.$newLineOnMySystem ,
$newLineOnMySystem, $yourString);

Read more here about string replacement:
http://nl3.php.net/manual/en/function.str-replace.php

But first find out what character(s) define newline on your system.

If uncertain try this:
You can use the function ord() to get the ascii value of a character.
Just print them out once for all characters in your file, and see what your
newline is.
More here:
http://nl3.php.net/manual/en/function.ord.php
Regards,
Erwin Moller

Sep 5 '06 #2
Yes, But how do i get those last 3 lines before the blanc line???

Sep 5 '06 #3
kulet wrote:
Yes, But how do i get those last 3 lines before the blanc line???
Oh, I assumed, without any valid reason, that you only had 2 newlines.

The poor mans solution would be to do both:
\n\n\n -\n
and
\n\n -\n

But then again you come here and ask how to do 4 lines, right? ;-)

That can be solved easily by doing something more smarter, eg write a simple
program that scans for repetive \n and replace them with one \n, only using
basic stringfunctions.

But much easier would be to use a regular expression that can recognize any
number of repeated \n.

Use a function like preg_replace(), read more here:
http://nl3.php.net/manual/en/function.preg-replace.php

Regards,
Erwin Moller
Sep 8 '06 #4

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

Similar topics

14
by: Daniel Chartier | last post by:
Hello. I work in the paper industry and we recently had someone (the original author) from within the company make a program for preventive maintenance. However, it had some bugs and we wanted...
7
by: GG | last post by:
Hi all, Here's an idea for a framework I thought about because I can make use of it in my project. Maybe such a thing exists, if so, I'd like pointers; if not, I'll start working on it myself. ...
8
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very...
34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
7
by: winlinchu | last post by:
Hi! I use Python, and writing some extension modules I think which could be written an C compiler, useful only to compile extension modules (I not think an GCC!!!!), so that the user not have to...
1
by: Lipei | last post by:
I have once try IntelliJ IDEA.And I can refactor easily,I can add try and catch just by a few clicks.It also can help me analazy the program's error.(e.g It warned me that I did't initialize the...
12
by: Paul H | last post by:
A little off topic this one because the database may not be written in Access. I am just looking for some advice.. I have an idea to help prevent a particular type of crime, a database will be...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
28
by: onkar | last post by:
This idea might be vey crazy. But I hope to get answers to this .. from comp.lang.c If a compiler is designed such that it automatically adds a free() matching every malloc() then is it not a...
2
by: pigeonrandle | last post by:
Hi, My application creates a project that is structured like a tree, so i want to use a treeview to display it to the user. Would it be a good idea to create the various parts of project as...
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
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
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
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
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,...

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.