473,884 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove all lines starting with b

Say I have the following file - dummy.txt:

a 4treg
a sdfas
a 4egdsag
b 1111
a ergdsfg
b 1111
c waefasdf

How would I go about removing all the lines that begin with a b? I
thought the following script (which I wrote with the intention of
replacing all such lines with the empty string) would but apparently it
doesn't:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^b.*?\n/','',$contents) ;
?>

Any ideas?

Mar 27 '06 #1
9 9144
Rex
I believe you want to read the file line by line and then try and match
your regex to each line. try:

$handle = @fopen("dummy.t xt", "r");
if ($handle) {
while (!feof($handle) ) {
$buffer = fgets($handle);
if( !preg_match('/^b.*?\n/',$buffer) )
echo $buffer . "<br>";
}
fclose($handle) ;
}

Mar 27 '06 #2
d
"Rex" <er********@gma il.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
I believe you want to read the file line by line and then try and match
your regex to each line. try:

$handle = @fopen("dummy.t xt", "r");
if ($handle) {
while (!feof($handle) ) {
$buffer = fgets($handle);
if( !preg_match('/^b.*?\n/',$buffer) )
echo $buffer . "<br>";
}
fclose($handle) ;
}


You could use substr($buffer, 0, 1)=="b" as opposed to preg_match, as
preg_match is a bit more intensive than just substr...
Mar 27 '06 #3
yawnmoth wrote:
Say I have the following file - dummy.txt:

a 4treg
a sdfas
a 4egdsag
b 1111
a ergdsfg
b 1111
c waefasdf

How would I go about removing all the lines that begin with a b? I
thought the following script (which I wrote with the intention of
replacing all such lines with the empty string) would but apparently it
doesn't:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^b.*?\n/','',$contents) ;
?>

Any ideas?


Hi,

Read this:
http://nl3.php.net/manual/en/referen....modifiers.php

So you could add the m modifier, like this:
echo preg_replace('/^b.*\\n/m','',$contents );

where:
^b means begins with b
..* means: anything untill end of line (\n) is reached
\\n means the end of line
and the m outside the // means that your string is treated the way you ment
too (see description at www.php.net).

Regards,
Erwin Moller

Mar 27 '06 #4
d wrote:
"Rex" <er********@gma il.com> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
I believe you want to read the file line by line and then try and match
your regex to each line. try:

$handle = @fopen("dummy.t xt", "r");
if ($handle) {
while (!feof($handle) ) {
$buffer = fgets($handle);
if( !preg_match('/^b.*?\n/',$buffer) )
echo $buffer . "<br>";
}
fclose($handle) ;
}


You could use substr($buffer, 0, 1)=="b" as opposed to preg_match, as
preg_match is a bit more intensive than just substr...

or just $buffer[0]...

-david-

Mar 27 '06 #5
Message-ID: <44************ ***********@new s.xs4all.nl> from Erwin Moller
contained the following:
Read this:
http://nl3.php.net/manual/en/referen....modifiers.php

So you could add the m modifier, like this:
echo preg_replace('/^b.*\\n/m','',$contents );

where:
^b means begins with b
.* means: anything untill end of line (\n) is reached
\\n means the end of line
and the m outside the // means that your string is treated the way you ment
too (see description at www.php.net).


I really have to learn more about regex. In the meantime...

<?
$contents = file('dummy.txt ');
foreach($conten ts as $value){
echo ($value{0}!="b" )? $value."<br>" : "";
}
?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 27 '06 #6

Erwin Moller wrote:
yawnmoth wrote:

<snip>

Hi,

Read this:
http://nl3.php.net/manual/en/referen....modifiers.php

So you could add the m modifier, like this:
echo preg_replace('/^b.*\\n/m','',$contents );

where:
^b means begins with b
.* means: anything untill end of line (\n) is reached
\\n means the end of line
and the m outside the // means that your string is treated the way you ment
too (see description at www.php.net).


Thanks for the suggestion - that helped!

I tried modifyng it such that it'd delete all lines not begining with a
b but am once again having some difficulty...

First, here's the (new) script:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^(?!b).*?\n/m','',$contents );
?>

The (?!b) should, as I understand it, match everything but b. What
instead seems to be happening is that, when using the dummy.txt file
that was posted earlier, all lines begining with a are deleted, but the
lines with c are still there.

Now, I realize that in this example, I could just as effectively
replace (?!b) with [^b] but this approach fails if b is anything other
than a single character. (eg. if I wanted to replace b with
192.168.1.1, I couldn't use [^...] - I'd have to use something like
(?!b))

Mar 27 '06 #7

yawnmoth wrote:
Erwin Moller wrote:
yawnmoth wrote:

<snip>

Thanks for the suggestion - that helped!

I tried modifyng it such that it'd delete all lines not begining with a
b but am once again having some difficulty...

First, here's the (new) script:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^(?!b).*?\n/m','',$contents );
?>

The (?!b) should, as I understand it, match everything but b. What
instead seems to be happening is that, when using the dummy.txt file
that was posted earlier, all lines begining with a are deleted, but the
lines with c are still there.

Now, I realize that in this example, I could just as effectively
replace (?!b) with [^b] but this approach fails if b is anything other
than a single character. (eg. if I wanted to replace b with
192.168.1.1, I couldn't use [^...] - I'd have to use something like
(?!b))


Sorry for posting, again. I just thought I should add that I'm trying
to do this with regular expressions, specifically, in an attempt to
better my understanding of them. Using another approach that doesn't
involve regular expressions would get the job done but would leave me
still kinda clueless as to why (?!b) doesn't work.

Mar 27 '06 #8

yawnmoth wrote:
Erwin Moller wrote:
<snip>

Thanks for the suggestion - that helped!

I tried modifyng it such that it'd delete all lines not begining with a
b but am once again having some difficulty...

First, here's the (new) script:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^(?!b).*?\n/m','',$contents );
?>

The (?!b) should, as I understand it, match everything but b. What
instead seems to be happening is that, when using the dummy.txt file
that was posted earlier, all lines begining with a are deleted, but the
lines with c are still there.

Now, I realize that in this example, I could just as effectively
replace (?!b) with [^b] but this approach fails if b is anything other
than a single character. (eg. if I wanted to replace b with
192.168.1.1, I couldn't use [^...] - I'd have to use something like
(?!b))


Never mind - I got it working:

<?
$contents = file_get_conten ts('dummy.txt') ;
echo "\r\n";
echo preg_replace('/^(?!b).*?(?:\n| $)/m','',$contents );
?>

The problem was due to the fact that the last line - which in dummy.txt
didn't start with a b - didn't end with a new line character but rather
with a EOF character (or maybe not a character at all? is EOF actually
a character?)

Mar 27 '06 #9
Geoff Berrow wrote:
Message-ID: <44************ ***********@new s.xs4all.nl> from Erwin Moller
contained the following:
Read this:
http://nl3.php.net/manual/en/referen....modifiers.php

So you could add the m modifier, like this:
echo preg_replace('/^b.*\\n/m','',$contents );

where:
^b means begins with b
.* means: anything untill end of line (\n) is reached
\\n means the end of line
and the m outside the // means that your string is treated the way you
ment too (see description at www.php.net).


I really have to learn more about regex. In the meantime...

<?
$contents = file('dummy.txt ');
foreach($conten ts as $value){
echo ($value{0}!="b" )? $value."<br>" : "";
}
?>


Personally I prefer non-regex solutions too.
Regex is ok as long as I am able to read what it says, and the bigger
complex regex always give me headaches. :P

Regards,
Erwin Moller
Mar 28 '06 #10

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

Similar topics

1
7718
by: a | last post by:
I'm trying to remove specific lines from text files. I need to always remove lines 1 thru 8 and lines 10 and 11 from text files. Seems like a simple task, but I'm not getting it. I'm reading the file via webresponse as a stream and then trying to remove the lines before parsing it and then writing it to an sql table. Any ideas on how I can accomplish the line removal/deletion?
2
9606
by: Becker | last post by:
This is probably a very dumb question, I am using VB.NET 2003 and I see where I can use a method to append text to a textbox, but how about remove? I have a textbox that I continually post log type data to and I want to keep it limited to 500 lines of text so that old lines get removed. How can I remove lines of text from a textbox? Thanks, Ben
4
10644
by: anne001 | last post by:
Hi For a class, students are going to run an experiment on line. Each time a subject runs, his/her data is appended to one giant text file. Their own data set will be just one line starting with the keyword they gave as identification. The faculty does not want the students to be able to download and see the giant data file. He wants the students to only download and see the data that starts with their own identification tag.
8
1471
by: openopt | last post by:
I don't know to which forum should I post the message I hope someone related to the Python kernel development will read & consider the idea I'm (a former? meanwhile not sure) MATLAB user & it's very annoing typing each time for example while i: print i ... instead of while i
1
1500
by: derik | last post by:
I hav a text file with N number of lines . i am able to read the file till the end .. in between many lines are starting with #. these lines starting with # are to be omitted. pleast help me in removing the lines starting with #
2
2254
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 1235635326235125134534 Recipe
1
1691
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 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,
8
4204
by: Umesh | last post by:
Please help. THANKS.
2
2822
by: lilly07 | last post by:
Hi, I have a 10 million record in a tab delimited text file (data.txt) and I have another file with line numbers (line_num.txt). My data.txt is as follows first 1 res 234 sec 23 fre 890 dec 26 der 690 ...
0
9954
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10769
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10869
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10426
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7985
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.