Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete rows from a txt file

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello. I have yet another question about CGI.<br><br>Say I have a text file with a user name and a password such as:<br><br>user : pass<br>user2 : pass2<br>user3 : pass3<br><br>And, then through a form, I have a text field that allows you to enter the user name in a text field named user.<br><br>I know how to add a user to a txt file, that part is easy, but does anyone know how to delete a user from a text file. And, if you can not delete the user, does anyone know how to delete a line of a text file?<br><br><br>If anyone could help, I would be very appreciative.<br><br>Thanks in advance.<br><br><br>
 
<FONT FACE=monospace><br>#!perl<br>$file = 'users.txt';<br># read file into var called buffer.<br>open(IPF,&quot;&lt;$file&quot;) ¦¦ die &quot;Failed to open InPut File, $!\n&quot;;<br>while (&lt;IPF&gt;) { $buffer .= $_; }<br>close IPF;<br><br># split lines into an array<br>@lines = split(/\n/,$buffer);<br><br># once you have identified the line you want removed, maybe line 5<br># use 'splice'<br>splice(@lines,4,1);<br><br>#write modified file<br>open(OPF, &quot;&gt;$file&quot;) ¦¦ die &quot;Failed to open OutPut File, $!\n&quot;;<br>print OPF &quot;@lines&quot;;<br>close OPF;<br><br></font><br><br>hope this helps<br><br> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
goBoating:<br><br>Thank you, that really did help. <br><br>One more question if you dont mind. <br><br><br>Say you enter the name 'user' into a form, and it opens up the database, see's if the names user exists, and if so, deletes it from the txt file?<br><br>Is that extremely hard to do or what?<br><br><br>Thanks for all the help though.
 
Reasonably straight forward.....just keep chopping process down until you get to descrete functional chunks and then write the chunks......The following is an over simplified example of either printing the input page or catching the submitted data and adding it to a text file.&nbsp;&nbsp;A lot is missing - probably should check that name and password exists if submit button is pushed........need to think about maybe using 'flock' on file to prevent users walking on each other..........need to check that output file exists before trying to open it for read......etc.....hopefully the illustration of the flow is useful.<br><br><FONT FACE=monospace><br>#!/usr/local/bin/perl -w<br>use CGI;<br>$thisCGI = '/cgi-bin/develop/junk.cgi'; # <font color=red> change to point to yours</font><br>$query = new CGI;# <font color=red>new CGI object catches submitted data.</font><br>print $query-&gt;header;<br>print $query-&gt;start_html(-title=&gt;&quot;Title of Page&quot;);<br>if ($query-&gt;param('dowhat') eq 'submit')<br>&nbsp;&nbsp;&nbsp;&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp; # <font color=red>got some data - so - do file stuff - should check that <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# name and password exist.&nbsp;&nbsp;If not -&gt; Fail.</font><br>&nbsp;&nbsp;&nbsp;&nbsp; $outputFile = 'cgiOut.txt';<br>&nbsp;&nbsp;&nbsp;&nbsp; # <font color=red>should check if file exists here - assuming file already exists.</font><br>&nbsp;&nbsp;&nbsp;&nbsp; open(OPF,&quot;&lt;$outputFile&quot;) ¦¦ print &quot;Failed to open OPF, $!&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp; while (&lt;OPF&gt;) { $buf .= $_; } # <font color=red> read file contents</font><br>&nbsp;&nbsp;&nbsp;&nbsp; close OPF;<br>&nbsp;&nbsp;&nbsp;&nbsp; @lines = split(/\n/,$buf); # <font color=red> split string into lines<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#For data management chores, you would split lines into values.... <br>&nbsp;&nbsp;&nbsp;&nbsp; # play with the data as you will<br>&nbsp;&nbsp;&nbsp;&nbsp; # maybe,... add the new name and password or use splice as<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;previously discussed</font><br>&nbsp;&nbsp;&nbsp;&nbsp; $name = $query-&gt;param('name');<br>&nbsp;&nbsp;&nbsp;&nbsp; $pass = $query-&gt;param('passwd');<br>&nbsp;&nbsp;&nbsp;&nbsp; $line = $name.'¦'.$pass.&quot;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp; push @lines,$line;<br>&nbsp;&nbsp;&nbsp;&nbsp;#<font color=red>write final version of array to output file.</font><br>&nbsp;&nbsp;&nbsp;&nbsp; open (OPF,&quot;&gt;$outputFile&quot;) ¦¦ print &quot;Failed to open OPF, $!&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp; foreach $line (@lines) { print OPF &quot;$line\n&quot;; }<br>&nbsp;&nbsp;&nbsp;&nbsp; close OPF;<br>&nbsp;&nbsp;&nbsp;&nbsp;#<font color=red>Say something nice to your user.</font><br>&nbsp;&nbsp;&nbsp;&nbsp; print $query-&gt;h3(&quot;Thank You, your data has been submitted.&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>else<br>&nbsp;&nbsp;&nbsp;&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp; #<font color=red> no data exists so - print front page</font><br>&nbsp;&nbsp;&nbsp;&nbsp; print $query-&gt;start_form(-action=&gt;&quot;$thisCGI&quot;, -method=&gt;&quot;POST&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp; print 'Name: ',$query-&gt;textfield(-name=&gt;'name', <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-size=&gt;25, -maxlength=&gt;35),'&lt;BR&gt;';<br>&nbsp;&nbsp;&nbsp;&nbsp; print 'Password: ',$query-&gt;password_field(-name=&gt;'passwd',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-size=&gt;10,-maxlength=&gt;15),'&lt;BR&gt;';<br>&nbsp;&nbsp;&nbsp;&nbsp; print $query-&gt;submit(-name=&gt;'dowhat', -value=&gt;'submit');<br>&nbsp;&nbsp;&nbsp;&nbsp; print $query-&gt;end_form;<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>print $query-&gt;end_html;<br></font><br><br>I ran this on my box, then cut and pasted here....but, have also added some comments....hopefully the edits have not boogered any syntax.&nbsp;&nbsp;It aught to be pretty close anyway.&nbsp;&nbsp;'Hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
goBoating:<br><br>I can't say Thanks enough with how much you helped me. <br><br>One question, not about programming though, hehe. Well, maybe it is, but where did you learn all that you know? Is it from years of experience, or have you purchased just about every Perl book out there, like I am trying to do?<br><br>Anyway, again, thank you so much for your help.
 
I don't feel like I know that much...just happy to share.&nbsp;&nbsp;I have found that using Perl means constantly&nbsp;&nbsp;looking for new solutions to newer problems.&nbsp;&nbsp;I used Perl to talk to an Oracle DB for several years.....got to where I thought I could handle Perl and SQL pretty well and then changed jobs to do this CGI stuff.....Started learning all over again.&nbsp;&nbsp;Once you get to where you can do a chore, you are ready to step to the next level.&nbsp;&nbsp;That makes discussions like tek-tips very valuable.&nbsp;&nbsp;<br><br>Unfortunately, what I know, I have picked up as I have gone along.......learning what was necessary to do the next job.&nbsp;&nbsp;That having been said (written), take heart...Perl lends itself to that approach like no other language.&nbsp;&nbsp;With a basic set of tools/skills, you can use some brutally simple approaches to problems and get some good solutions......maybe not the best or most sophisticated, but good and effective.<br><br>About 5 years back, I started out using the SAMS Teach Yourself in 21 book.&nbsp;&nbsp;It was a good starter with clear (simple) examples and good descriptions.&nbsp;&nbsp;It was a very quick read. After that, the CAMEL books <i>Programming</i> and <i>Advanced Programming</i> gave some real horse power.&nbsp;&nbsp;They were, after all, written by the founding fathers.<br><br> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Hey:<br><br>Yeah, I love the O'Reilly set of books. I just recently purchased the Perl Cookbook and Learning Perl On Win32 Systems, since I am on Windows 2000. I find both to be very useful.<br><br>Do you personally know any of the founding fathers, aka, Larry Wall or Tom Christianson?<br><br>I want to go to the Perl Confrence, but I can't do to lack of money. Have you ever been?<br><br>I, also love to help people, and try to help all I can. You defiantly have my vote for Tipmaster of the Week.<br><br><br>Thanks a lot.<br><br><br>Vic.
 
The Camel (otherwise known as the Perl Bible) is definitely the way to go. Another note... one of the authors, Randal L. Schwartz, writes a nice column every month in Web Techniques magazine- a great magazine for learning a lot of different things. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
I told a slight untruth above (I have had a little instruction).&nbsp;&nbsp;I did take one one week class from Tom Christiansen at XOR Engineering in Boulder, Co.&nbsp;&nbsp;I doubt he would know me from Adam's house cat.&nbsp;&nbsp;Other than that, don't know any of the fathers.&nbsp;&nbsp;Never been to a conference....Like you, I can't find anyone to pay my way.&nbsp;&nbsp;I would rather spend my time working on my boat anyway.<br><br> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top