Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Deleting Text in Perl

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 08-14-2006   #1 (permalink)
Linux Engineer
 
Thrillhouse's Avatar
 
Join Date: Jun 2006
Location: Arlington, VA, USA
Posts: 1,365
Deleting Text in Perl

Is there a simple function to clear the contents of a text file in Perl? All of the documentation I've seen on file manipulation tells you how to delete the file itself (i.e. unlink()) but I need the file to remain in existence, just with nothing in it. Thanks for your help.
Thrillhouse is offline  


Reply With Quote
Old 08-14-2006   #2 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 814
Hi.

Experiment with the truncate function ... cheers, drl

Code:
truncate filehandle, length
truncate expr, length
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Old 08-15-2006   #3 (permalink)
Linux Engineer
 
Javasnob's Avatar
 
Join Date: Jul 2005
Location: Wisconsin
Posts: 942
Also, in most languages, if you open a file for writing, it gets truncated right away.
__________________
Flies of a particular kind, i.e. time-flies, are fond of an arrow.

Registered Linux User #408794
Javasnob is offline   Reply With Quote
Old 08-15-2006   #4 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 814
Hi, Javasnob.

Yes, good point.

The truncate function provides more flexibility as well as conveying the developer's intention to the reader, but I do like the simplicity of open-write.

I quickly checked truncate and open-write in perl, and they both performed as expected, keeping the same inode for the file, effectively emptying the file (I seem to recall that we had used the phrase zeroing a file for such an operation, as in a null command, but with a re-direction, just ">t1", for example) ... cheers, drl
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Old 08-15-2006   #5 (permalink)
Linux Engineer
 
Thrillhouse's Avatar
 
Join Date: Jun 2006
Location: Arlington, VA, USA
Posts: 1,365
What if I want to clear the text from a file that I'm reading from? Do I have to open it in a different way? Basically, what I'm doing is reading all of the lines from a file and when I'm done I want the file to be empty. Also, I can't seem to figure out what the second parameter of the truncate function is. The documentation says length but is that lines or characters or what? Thanks again for your help.
Thrillhouse is offline   Reply With Quote
Old 08-15-2006   #6 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 814
Hi.

The second argument on truncate is the number of characters to which you wish to truncate the file. I tested this by creating a short file, then running a perl program that opened the file, read the contents into an array, used truncate with a request of 0 characters, and it worked as I expected (I also truncated to a non-zero length to be sure that it was a character length). I did notice one anomaly, and I haven't had time to investigate it fully -- the truncate function did not seem to work when I used the filehandle, only when I used the string that was the filename (I used a scalar to hold the string).

You could also try the open-write method -- so you'd open the file, read it, close it, open it for write, close it.

I suggest you write a little script or two to test these methods before embedding it into a longer code. That way you can feel confident that possible problems are not due to the truncate ... cheers, drl

( edit 1: typo )
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Old 08-15-2006   #7 (permalink)
Linux Engineer
 
Thrillhouse's Avatar
 
Join Date: Jun 2006
Location: Arlington, VA, USA
Posts: 1,365
Quote:
Originally Posted by drl
I did notice one anomaly, and I haven't had time to investigate it fully -- the truncate function did not seem to work when I used the filehandle, only when I used the string that was the filename (I used a scalar to hold the string).
Ah, thanks drl. That was my problem, I was using the file handle. Worked when I used the filename. Strange.
Thrillhouse is offline   Reply With Quote
Old 08-15-2006   #8 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 814
Hi.

I have an answer, not thoroughly tested, but it makes sense.

To use function truncate on a filehandle seems to require that the file be open with write permission. That seems to be right (or write, as the case may be ).

My test was to unlink a file to make sure it did not exist, open it for write, put some data on it (more than 10 characters), then truncate it (I used 10 characters), finally closing it -- and it did that exactly. There may be other factors involved, but I'm OK with this answer ... cheers, drl
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Old 08-15-2006   #9 (permalink)
Linux Engineer
 
Thrillhouse's Avatar
 
Join Date: Jun 2006
Location: Arlington, VA, USA
Posts: 1,365
I'm OK with using the explicit file name. I can't unlink the file so I'll just do it that way. Good to know the reason for it though.

Thanks.
Thrillhouse is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:45 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2