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:
delete line of text from txt doc

 



gerble1000
User

Aug 10, 2012, 12:39 PM

Post #1 of 7 (2492 views)
delete line of text from txt doc Can't Post

i have a txt document that has over 3000 lines with username and passwords inside, example
username,password,name,number,name,number
jack,123pass,name,number,name,number
steve,123password,alan,07878787877,name,number

i could do with a cleanup script to delete the lines that do not contain numbers after the username and password,
so in this example above i would need to remove the lines

username,password,name,number,name,number
jack,123pass,name,number,name,number

could someone please help as this is really slowing my website down,thankyou :)


(This post was edited by gerble1000 on Aug 10, 2012, 12:58 PM)


Laurent_R
Veteran / Moderator

Aug 10, 2012, 1:42 PM

Post #2 of 7 (2488 views)
Re: [gerble1000] delete line of text from txt doc [In reply to] Can't Post

You don't give enough information, so, I'll give a try, but not sure this will filt the bill.

Assuming you are reading the file:



Code
while (<IN_FILE>) 
(my $one, $two, $three) split /,/, $_;
next unless defined $three; # $three undefined => line to be removed
# code to keep the other users...
}



gerble1000
User

Aug 10, 2012, 1:53 PM

Post #3 of 7 (2485 views)
Re: [Laurent_R] delete line of text from txt doc [In reply to] Can't Post

dont think that will work but here is another way of looking at it.

i would need to remove every line in the file that ends with this exactly
name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number

and the username,password, will be in front of that string like so
frank,mypassword,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number

they are login details and that is why i cannot show the entire file
if the line contains names and numbers apart from the username and password then the line needs to be kept.
i hope this explains it better.

im thinking
grep /name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number,name,number/
then remove line somehow.
i am not clued up with the coding :)


Laurent_R
Veteran / Moderator

Aug 11, 2012, 1:11 AM

Post #4 of 7 (2467 views)
Re: [gerble1000] delete line of text from txt doc [In reply to] Can't Post

My assumption, for the code I have submitted, is that you want to keep the line if it has only two words and remove it if it has more than two words.

Your new explanation does not contradict my original assumption. So, for me, the code I have presented answers your reqsuest.


gerble1000
User

Aug 11, 2012, 1:27 AM

Post #5 of 7 (2465 views)
Re: [Laurent_R] delete line of text from txt doc [In reply to] Can't Post

the $one $two $three split will return defined as the username password and the name.
if you could provide an example i coult test it :)


Laurent_R
Veteran / Moderator

Aug 11, 2012, 2:13 AM

Post #6 of 7 (2463 views)
Re: [gerble1000] delete line of text from txt doc [In reply to] Can't Post

I am testing is $three is defined.

If you run the split against the line "name, password", $three will be undefined; if you run it against "name, password, other_name", $three will be defined.

Then, you only need to decide to print the line or not to print it depending on whether or not $three is defined.

If I understand correctly what you want, that should do the trick.

Looking better at your requirement it appears that my test was done in the wrong way. It should probably be:


Code
next if defined $three; # $three defined => line to be removed



(This post was edited by Laurent_R on Aug 11, 2012, 2:17 AM)


gerble1000
User

Aug 12, 2012, 2:31 AM

Post #7 of 7 (2444 views)
Re: [Laurent_R] delete line of text from txt doc [In reply to] Can't Post

i ended up doing it by using notepad++ and there macro recorder, it took 2 minutes :)

 
 


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

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