0

i have a huge .java file and i want to find all declared objects given the className. i think the declaration will always have the following signature:

className objName;

or

className objName =

or

className objName=

can someone suggest me a grep pattern which will find these signatures. I have the following (incomplete) :

    cat $rootFile | grep "$className " 

EXAMPLE :

If the input file is :

Policy pol1;
Policy pol2 ;
Policy   pol3  ;
Policy pol4=new Policy();
Policy pol5 = new Policy();
Policy pol6= new Policy();

I want to extract the following list :

pol1
pol2
pol3
pol4
pol5
pol6
1
  • It's not necessary to use cat. Both grep and Perl (used in an answer below) accept filenames as arguments. Also, grep is not part of Bash so there's no such thing as "bash grep". Commented May 21, 2010 at 3:43

1 Answer 1

3

Probably perl can help here

cat $rootFilename | perl -pe 's/Policy[ \t]+([a-zA-Z0-9_]+)[ \t]*[;=].*/\1/g'

Using sed, can be done as well

sed -e 's/Policy[ \t]\+\([a-zA-Z0-9_]\+\)[ \t]*[;=].*/\1/g' $rootFilename
Sign up to request clarification or add additional context in comments.

2 Comments

Useless use of cat. Otherwise +1.
Great thanks. This one work. Thanks to everyone for the contribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.