CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Search and remove

 



Fredrik
Deleted

Jan 25, 2001, 12:11 AM

Post #1 of 6 (673 views)
Search and remove Can't Post

Hi
I have not been able to fix the problem yet.
We can make it little more simple like this,I have this text file that I
want to remove some stuff in.

The text file looks like this

12
12
13
15
17
19
21
25
25
30
30

How do I do if I want to remove everything except the number 25.
There is 2 numbers of 25.

So when the program is finished it should look like this

25
25

Everything except 25 is removed and then printed back to my text file
I have tried to find it in the manual but I didīt find anything about it.

Big thanks in advance



kencl
User

Jan 25, 2001, 1:52 AM

Post #2 of 6 (672 views)
Re: Search and remove [In reply to] Can't Post

Try this:

Code
open(FH, "<InputTextfilename.txt") or die $!; 
@Input = <FH>;
close(FH);
$KeepOnly = 25; # so you can set this to whatever you want
@Output = grep(/$KeepOnly/, @Input);
open(FH, ">OutputTextfilename.txt") or die $!;
for (@Output) {
chomp($_); # I'm never sure if I have to do this or not, so I always do...
print FH "$_\n";
}
close(FH);

note: this will return any line with the number 25 anywhere in it, so if you have a line like

Code
23 
a bunch of extra 25 text
25 bottles of beer on the wall
oh, better watch out for this 25

the last 3 lines will be returned.

If you want only line beginning with 25 to be returned, change /$KeepOnly/ to /^$KeepOnly/ On my example file, only the line "25 bottles of beer on the wall" would be returned.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


Fredrik
Deleted

Jan 25, 2001, 5:41 AM

Post #3 of 6 (666 views)
Re: Search and remove [In reply to] Can't Post

Thanks but this will only keep one line with 25.
I want to print all lines that contains 25 like this
in the textfile

25
25

Thanks 4 helping




pu_nk
journeyman

Jan 25, 2001, 7:52 AM

Post #4 of 6 (663 views)
Re: Search and remove [In reply to] Can't Post

Here.. This may elp you out..

What you have to do is "push" the items you want out of the DB into another array ..
the code

open (DELETE, "data.txt");
flock DELETE, 2;
@delete = <DELETE>;
flock DELETE, 8;
close(DELETE);
foreach $delete (@delete) {
chomp $delete;
($itme1,$item2$item3)=split(/::/,$delete);



push (@new_array,$delete)unless ($item1 eq "25");
}


open (FILE, ">data.txt");
flock FILE, 2;
foreach $new_variable (@new_array) {
print FILE "$new_variable\n";
}
flock FILE, 8;
close(FILE);

hope this helps and dont forget to add some error checking in the code...

Drew



zanardi
journeyman

Jan 25, 2001, 2:29 PM

Post #5 of 6 (653 views)
Re: Search and remove [In reply to] Can't Post


In Reply To
chomp($_); # I'm never sure if I have to do this or not, so I always do...

chomp removes carriage returns off the end of strings, it's not neccessary unless your using a string that has a carriage return on it...

anyway, this is how I would do what you wanted to do:


Code
open(D, "data.txt") or die $!; 
my @delete = <D>;
close(D);

foreach (@delete) {
chomp($_);
push (@numbers,$_) if ($_ == 25);
}

open(F, ">data.txt") or die $!;
print F join("\n",@numbers);
close F;

and pu_nk you need to type slower or something, there are alot of errors.


My BBS


kencl
User

Jan 26, 2001, 10:22 AM

Post #6 of 6 (638 views)
Re: Search and remove [In reply to] Can't Post

No, this will keep all lines with 25. grep returns an array containing every line where the expresssion has a match. There's a good article on grep. in the learning center.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives