awk assistance

pll at lanminds.com pll at lanminds.com
Wed Nov 13 15:14:51 EST 2002


In a message dated: Wed, 13 Nov 2002 13:30:28 EST
"Price, Erik" said:

>> Why must it be lengthy?
>>
>> 	find ./ -type f | perl -F'/' -ane  'print "$F[$#F]";'
>>
>> seems to do the trick just fine.  And if you want to weed out=20
>> duplicates pipe the output through 'uniq' with your choice of swiches.
>
>
>My own one-liner would have been lengthy.  It would have gone
>something like (untested):
>
>cat findoutput.txt | perl -e \
> 'while(<STDIN>){m!([^/]+)$!;print $1}'| uniq

Well, the above code does exactly what you're attempting to do here.

Obviously the 'find ./ -type f' portion could be replaced with
'cat findoutput.txt', but that's an extra shell process better done:

	perl -e 'some perl code here' findoutput.txt

The rest of this code:

>> 	find ./ -type f | perl -F'/' -ane  'print "$F[$#F]";'

says:
	perl		- run the perl interpreter
	-F'/'		- same as awk, we're going to split on the 
			  '/' character

	-a		- turn on 'autosplit' mode and place the results of the 
			  split() into @F

	-n		- place a while loop around your code

			IOW, using the -an construct, assume the
			following loop around any other code:

				while (<>) {
				  @F = split(' ');
				  # your code goes here
				}


	-e 		- execute the following code snippet

	'print "$F[$#F]";'	- print the last element of the array @F

Since I used the -F to set the split character to '/' and -a to turn 
on autosplit mode, and -n create a loop around my code, what I end up 
with is this:

	while (<>) {
	   @F=split('/');
	   print $F[$#F];
	}

Which says:

	while there's still something to read
		split on the '/' and place the results into the @F array
		then print the last element of the array

the '$#' construct is used in perl to determine the last element in 
an array.  So, with any given array $arrayname[$#arrayname] indexes 
into the last element.

>I still haven't finished the Camel Book.  I'm not very good with
>Perl's command line switches and, without resorting to a man page,
>I still have no idea what the above code does! ;)

>>>>> On Wed, 13 Nov 2002, "Steven" == Steven W. Orr wrote:

  Steven> Everyone starts with the Camel book but everyone soon
  Steven> realizes that it's pretty crappy to try to learn from. It's
  Steven> best left as a reference book.  The BEST book for learing
  Steven> perl that I've found is Object Oriented Perl by Damian
  Steven> Conway.

I agree that Damian's book is fantastic and that the Camel is a 
reference, not a tutorial.  However, I don't know if I agree that 
Damian's book is the best for learning Perl.  There are a lot of
uses for perl which do not require OO, and will in fact, suffer
from using it.  It's been a while since I picked up Damian's book, 
but I don't recall thinking it a very good book for beginners.

IMO, the best way to learn any language is to just use it.
I picked up perl with nothing more then the on-line perl 
documentation.  I already knew the basics of programming, so I guess 
all I need was a reference to determine syntax, etc.

I don't think I ever picked up a copy of the Camel until well after I 
had been hacking perl for about 3 or 4 years.  But, I didn't need to, 
since the camel is nothing more than the perldocs on dead trees :)

If you really want to learn perl, or any other language, just use it.
When you get stumped, read the docs and ask questions when you don't 
understand the docs.  comp.lang.perl.* and the various perl-mongers 
mail lists are great forums for this!
-- 

Seeya,
Paul
--
	It may look like I'm just sitting here doing nothing,
   but I'm really actively waiting for all my problems to go away.

	 If you're not having fun, you're not doing it right!





More information about the gnhlug-discuss mailing list