Need commands - how to?

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Need commands - how to?

Post by alex.barylski »

Here is what I am trying to do:

1) I need a command which recursively scans a directory tree and collates each PHP scripts source and sends to screen:

Code: Select all

cat | find ./opt/lampp/htdocs/
The above doesn't work??? Everything I've Googled suggested using Perl in combination with cat. Is it not possible using CLI only? Ideally I would ike to use the cat option -n to display lines numbers for each file. Removal of comments using regex would be *huge* bonus. :D

2) Something similar to the above except I wish to extract strictly the class and function signatures. Basically:

Code: Select all

class MyBase extends MyChild{
AND

Code: Select all

function someMethod($p1, $p2){

Code: Select all

grep --recursive -n '[(^class(\w)+a-zA-Z(\w)+{)|(^function(\w)+a-zA-Z(\w)+{)]' /opt/lampp/htdocs/rapidcourier/*
This not surprisingly - doesn't work either, although my regex knowledge is limited...

I'd love to get these working, so any help is appreciated.

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look into using PHP's tokenizer.

General idea for (e)grep:

Code: Select all

feyd:/Volumes/Devel/Development feyd$ egrep -rni --include=*.php "^[[:space:]]*((abstract[[:space:]]+)?class|interface|(abstract[[:space:]]+)?((public|protected|private)[[:space:]]+)?function)[[:space:]]+[a-z_]+" ./
./Soverign/Library/Security/Permission.php:7:class DNAPermission extends DNAObject
./Soverign/Library/Security/Permission.php:12:  public function __construct( $foo = null )
./Soverign/Library/Security/Permission.php:23:  public function __get( $value )
./Soverign/Library/Types/Object.php:3:class DNAObject
./Soverign/Library/Types/Object.php:19: public function __construct( DNAProperty $property = null )
./Soverign/Library/Types/Object.php:41: protected function addProperty( $name, $value = null )
./Soverign/Library/Types/Object.php:55: protected function addPropertyEx( $name, $access, $value = null )
./Soverign/Library/Types/Property.php:3:class DNAProperty
./Soverign/Library/Types/Property.php:62:       public function __construct( $owner, $access = self::PROPERTY_PRIVATE )
./Soverign/Library/Types/Property.php:90:       public function set( $value )
./Soverign/Library/Types/Property.php:118:      public function get()
./Soverign/Library/Types/Property.php:137:      protected function allowed()
./Soverign/Library/Types/NameValue.php:4:class DNANamedSet extends DNASet
./Soverign/Library/Types/Set.php:9:class DNASet extends DNACollection
./Soverign/Library/Types/Set.php:18:    public function add( $element )
./Soverign/Library/Types/Set.php:36:    public function addAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:8:class DNACollection implements Iterator
./Soverign/Library/Types/Collection.php:31:     public function __construct( $input )
./Soverign/Library/Types/Collection.php:89:     public function rewind()
./Soverign/Library/Types/Collection.php:99:     public function current()
./Soverign/Library/Types/Collection.php:109:    public function key()
./Soverign/Library/Types/Collection.php:119:    public function next()
./Soverign/Library/Types/Collection.php:129:    public function valid()
./Soverign/Library/Types/Collection.php:141:    public function add( $element )
./Soverign/Library/Types/Collection.php:198:    public function addAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:221:    public function clear()
./Soverign/Library/Types/Collection.php:232:    public function contains( $element )
./Soverign/Library/Types/Collection.php:244:    public function containsAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:259:    public function isEmpty()
./Soverign/Library/Types/Collection.php:270:    public function remove( $element )
./Soverign/Library/Types/Collection.php:290:    public function removeAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:307:    public function retainAll( DNACollection $collection )
./Soverign/Library/Types/Collection.php:319:    public function size()
./Soverign/Library/Types/Collection.php:329:    public function toArray()
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You need to look into the Unix find command. The syntax is a little weird, but it will find only specific files in a tree and run commands on each.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

I'm using a PHP token iterator as we speak to extract that information but I figured why not try and keep it to CLI and native LInux commands, so I don't have to worry about dragging scripts around on each project.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

arborint wrote:You need to look into the Unix find command. The syntax is a little weird, but it will find only specific files in a tree and run commands on each.
I tried using find but couldn't figure out how to pipe commands from find to cat or visa versa... :?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

find ./ -name '*.php' -exec cat {} ';'
cat `find ./ -name *.php`
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Bulgarian Linux users rock!!! :)
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

:D :D :D

They really do !
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I this is what you're after:

Code: Select all

grep -nr --include='*.php' -E '(function[ \t\r\n\v\f]+&?[_a-zA-Z][_a-zA-Z0-9]*[ \t\r\n\v\f]*\(|class[ \t\r\n\v\f]+[_a-zA-Z][_a-zA-Z0-9]*.*\{)' .
add "| less" to it to view in a more human friendly way. I couldn't seem to get the POSIX bracket expressions such as [:space:] or back-references to work so that's why it's so long. I copped out at the end of the class match with .* where a more complete regex would match specifically for extends and implements etc.
Post Reply