3

Lets say I have a java class called DirectAction

some of the methods in the class have the word "action" or "Action"

What is the grep command to get the names of all the methods who have "Action" or "action" and write it to an external file?

Example:

public class DirectAction extends ERXDirectAction {

/**
 * 
 * @param aRequest
 */
public DirectAction(WORequest aRequest) {

}

// ============================================================================================
// ACTIONS

/**
 * Default Action
 * 
 */
@docDirectAction
public WOActionResults defaultAction() {

}

/**
 * @return goes to login page for student
 */
@docDirectAction
public WOActionResults studentAction() {
    );
    return nextPage;
}

/**
 * @return goes to Admin login page
 */
@docDirectAction
public WOActionResults adminAction() {
    return nextPage;
}

/**

 * @return calls the AdminAction
 */
@docDirectAction
public WOActionResults staffAction() {

    return adminAction();
}

}

I want to get only the below output.

public class DirectAction extends ERXDirectAction

public DirectAction(WORequest aRequest)

public WOActionResults defaultAction()

public WOActionResults studentAction()

public WOActionResults adminAction()

public WOActionResults staffAction()

NOTE: the only word that is common in all these methods is "Action" or "action"

Also please use only grep if possible :)

1
  • 1
    If possible, I suggest you run this grep command on the output of javap -private DirectAction.class instead of DirectAction.java, because that will give you just the method names. (javap is a printer for java bytecode.)
    – Riking
    May 28 '14 at 23:20
5

If you want to return only the matching text (instead of the whole line), you need the -o flag

grep -o 'public .*[a|A]ction.*)'

or with extended regular expressions

grep -Eo 'public .*[aA]ction.*\)' file

Or, extended for any lines beginning with pub(lic), pri(vate) or pro(tected)

grep -Eo '^(pub|pri|pro).*[aA]ction.*\)' file
2
  • Tried that too, but sometimes the methods could be private or protected... So that will become too many greps in one line. Which gets annoying .
    – gkmohit
    May 28 '14 at 19:27
  • grep -o 'p[a-z]* .*[a|A]ction.*)' DirectAction.java Would that work?
    – gkmohit
    May 28 '14 at 19:36
1

Not grep, but...

sed -n '\|DirectAction|{/^@/n;/{/s|||;p}'

Returns me these results:

public class DirectAction extends ERXDirectAction
public DirectAction(WORequest aRequest)
public WOActionResults defaultAction()
public WOActionResults studentAction()
public WOActionResults adminAction()
public WOActionResults staffAction()

Basically I just address lines that contain the phrase DirectAction. I then check to see if the first character on the line is an @ and - if so - I overwrite the current line with the next line. Next I look for a { and if found I remove it. Then I print.

0
grep -i action( <file>

The -i makes the search case insensitive, and I added the ( at the end of action because it appears that all the things you expect to see have a paren at the end, but you could just leave it out.

For the record, this is the simplest use of grep you'll find; reading the man page would have revealed this solution very quickly.

3
  • I did that :)It doesnt work :/
    – gkmohit
    May 28 '14 at 19:21
  • Care to be more specific?
    – HalosGhost
    May 28 '14 at 19:22
  • The output is not as expected, its printing the return adminAction() Which I dont need, then it is also returning "{" at the end. I want that to be ignored too.
    – gkmohit
    May 28 '14 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.