| |||||||||||
| |||||||||||
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 ![]() 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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |