Training, Open Source computer languages

This is page http://www.wellho.net/forum/Perl-Programming/Perl-rem ... lines.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Perl - remove unwanted lines

Posted by hycheah (hycheah), 8 October 2006
I have a text (list.txt) to process which looks like this:

.\hlee4\unix
.\hlee4\perl
file1.pl
file2.pl
file3.pl
.\hlee4\linux
file_a.txt
file_b.txt
.\hlee4\C++
.\hlee4\Verilog
file_x.v
file_y.v

The text file consists of the names of directories and the files in each directories. If the directory is empty, no files will be listed under it.

I have been trying to write a Perl script to remove the names of directories which has no files in it. I really have no idea how to do this.

The processed text should look like this:

.\hlee4\perl
file1.pl
file2.pl
file3.pl
.\hlee4\linux
file_a.txt
file_b.txt
.\hlee4\Verilog
file_x.v
file_y.v

with .\hlee4\unix and .\hlee4\C++ removed.

I have been looking at this for a few days now. HELP !!


Posted by admin (Graham Ellis), 9 October 2006
Read the file line by line.

If a line is a DIRECTORY name, save it into a variable and initilaise a counter to zero.

If a line is a FILE and the counter is zero, output the stored directory name
then increment the counter and print out the file name.

Posted by hycheah (hycheah), 9 October 2006
Thanks!! That was a great help.  

Posted by hycheah (hycheah), 9 October 2006
What if I wanted to sum up all the file's size?

.\hlee4\perl
file1.pl  2k
file2.pl  3k  
file3.pl  4k
.\hlee4\linux
file_a.txt  5k
file_b.txt  6k
.\hlee4\Verilog
file_x.v  2k
file_y.v  3k

I can print out the first 2 directories but not the last one.
It will look like this:

.\hlee4\perl
Total: 9k

.\hlee4\linux
Total: 11k

.\hlee4\Verilog

The total file size of the last directory will not be printed.
I think this has got to do with the loop placement.





Posted by admin (Graham Ellis), 9 October 2006
Yes, I think I see what you're doing; you're printing out the total wehn you find a new directory name.  Add the same code at the very end, once you've hit the end of the directory parsing - or better still, put the total printing code into a sub and call that sub once where the code itself currently is, and once where the second copy would go.

Posted by hycheah (hycheah), 10 October 2006
No no. I won't be parsing thru the directories physically.

I am processing the dir and files information from a text file. The text file will list the directories and all the files (including file sizes).

I need to print the dir name and sum up all the file size and print it.

And currently I am having trouble printing out the last total file size.

Posted by admin (Graham Ellis), 10 October 2006
Replace "directory parsing" with "file parsing" in my previous response, then.  Yes, I know you're doing it via a file.  The technique doesn't differ   - just the command names



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho