Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 :)

share|improve this question
    
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 at 23:20

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
    
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 . –  Unknown May 28 at 19:27
    
grep -o 'p[a-z]* .*[a|A]ction.*)' DirectAction.java Would that work? –  Unknown May 28 at 19:36

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.

share|improve this answer
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.

share|improve this answer
    
I did that :)It doesnt work :/ –  Unknown May 28 at 19:21
    
Care to be more specific? –  HalosGhost May 28 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. –  Unknown May 28 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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