Perl - remove unwanted lines
Posted by hycheah (hycheah), 8 October 2006I have a text (list.txt) to process which looks like this:
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:
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 2006Read 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 2006Thanks!! That was a great help.
Posted by hycheah (hycheah), 9 October 2006What if I wanted to sum up all the file's size?
I can print out the first 2 directories but not the last one.
It will look like this:
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 2006Yes, 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 2006No 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 2006Replace "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
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho