![]() ![]() |
Oct 12 2006, 03:23 PM
Post
#1
|
|
|
Apple Addict ![]() Group: Members Posts: 18 Joined: 29-September 06 Member No.: 86 |
Hi there,
Given a directory full of Fortran files, I want to list all the functions using grep. A Fortran function is declared as follow : CODE [type] function the_name(the_args,another_args,...) where [type] is optional. To sum up, one has to match 1) the beginning of the line 2) any number of spaces 3) any characters up to a space 4) the word function 5) the name of the function 6) the opening brace Steps 5) and 6) are here to avoid matchind the end function statement In Oniguruma, the following does the job : CODE (?ix)^\s*(.*)\s*\b(function)\b\s*[a-zA-Z0-9_]*\( but I don't know which RegExp format is used by grep (damn : it does not seem to know (.*) Any help would be much appreciated TIA |
|
|
|
Oct 18 2006, 12:57 PM
Post
#2
|
|
![]() Green Apple Group: MacBidouille Team Posts: 6 Joined: 29-September 06 From: Switzerland Member No.: 83 |
Hi guerom00 !
Why dont you just grep "function" | grep -v "end" ? You might have to add stuff to weed out "function" written in comments. This post has been edited by Philjfry: Oct 18 2006, 01:51 PM |
|
|
|
Oct 18 2006, 01:21 PM
Post
#3
|
|
|
Apple Addict ![]() Group: Members Posts: 18 Joined: 29-September 06 Member No.: 86 |
Hi guerom00 ! Why dont you just grep "function" | grep -v "end" ? You might have to add stuff out "function" written in comments. Thanks Philjfry It appears that CODE grep "function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" do what I want.CODE #!/usr/bin/env csh echo "Finding tag "$2" in directory "$1 cd "$1" if(-e .tags) goto exist grep "subroutine [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" > .tags grep "function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" >> .tags grep "entry [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" >> .tags echo >> .tags set nlines=`cat .tags | wc -l` @ nlines = $nlines - 1 if($nlines == 0) then echo 'No functions or subroutine found' exit 1 endif set files=`cat .tags | cut -f 1 -d :` set names=`cat .tags | cut -f 1 -d :` set subfun=`cat .tags | cut -f 2 -d :` rm .tags touch .tags set i=0 set j=0 while(1 < 2) @ i ++ if("$subfun[$i]" == "subroutine" || "$subfun[$i]" == "function" || "$subfun[$i]" == "SUBROUTINE"\\ || "$subfun[$i]" == "FUNCTION" || "$subfun[$i]" == "entry" || "$subfun[$i]" == "ENTRY") then @ j ++ @ k = $i + 1 set names[$j]=`echo "$subfun[$k]" | cut -f 1 -d "("` @ i = $k echo "$files[$j]""|""$names[$j]" >> .tags endif if($j == $nlines) break end exist: set file_name=`cat .tags | grep "|$2\b" | cut -f 1 -d "|"` echo "opening "$file_name if($file_name == '') exit set line=`grep -n "subroutine $2" "$file_name" | grep -v "end" | cut -f 1 -d :` echo 'line '$line if($line == '') set line=`grep -n "function $2" "$file_name" | grep -v "end" | cut -f 1 -d :` if($line == '') set line=`grep -n "entry $2" "$file_name" | grep -v "end" | cut -f 1 -d :` echo $line if($line != '') then mate -l $line $file_name else mate $file_name endif An equivalent, not perfect, very ugly ( I know : it could be more easy/beautiful/efficient using Ruby or Python but what can I do : I only know csh scripts Edo : The last refinement is CODE grep "^[^c\!].*function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" because I picked up some spurious things before…
This post has been edited by guerom00: Oct 18 2006, 10:38 PM |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 19th June 2013 - 11:43 AM |