Enum files in java can get messy in structure, I'm trying to get Just the Enum names

import com.mckesson.dex.model.activityLog.concreteFactory.ModifiedPropertyYesNoFactory;

public enum OrganizationActivityLogItemType implements IActivityLogItemType<OrganizationActivityLog>, DisplaysAs

    @Deprecated GENE_VARIANT_PUBLIC( "Gene Variant/Mutation/Allele", null ),

    VARIANTS_MUTATIONS( "Variants/Mutations/ Alleles/Loci/Probes", ALFactory.PUBLIC_PRIVATE ),
    CATEGORIES( "Categories", null ),
    PUBLIC_VISIBLITY_COVERED_WHEN_MEETS_NECESSITY_CRITERIA( "Covered - When Meets Medical Necessity Criteria",
                                                            ALFactory.PUBLIC_PRIVATE ),
        "Not Covered - Investigational",
        ALFactory.PUBLIC_PRIVATE ),
        "Not Covered - Not Medically Necessary",
        ALFactory.PUBLIC_PRIVATE ),

This is close-ish, but I feel like it could probably be done better.

awk '{ print $1 }' My.java | grep -v '^"' | grep -v '^ALFactory' | sed -e 's/(//'

How can I get just the Enum names from an enum file using unix cli tools?

solutions need to be portable to OS X at least, as that's my working environment, though I would prefer that it also work on Linux.

1 Answer 1


Here is a script in perl that does it. Doing it just piping commands in the command line is either not possible or not maintainable. A naive approach would be

perl -npe 's/\b|\s+/\n/g' < My.java | grep '^[A-Z]\+_[A-Z_]\+'

but the script below is better:

#!perl -w
my $content = do { local $/; <> };
my @tokens = split /\b|\s+/, $content;
my $n = 0;
my $skip;

foreach (@tokens) {
    $n++; $skip = 0;
    next if /\s+/;
    next if /"/ ... /"/;    # skip strings
    next if /\(/ .. /\)/;   # skip parameters
    next if /@/ .. /^\w+$/; # skip annotations
    $skip ||= 1  if $n == 1 .. /\benum\b/; # skip from beginning to enum
    $skip ||= 1  if /enum/ .. /\{/; # skip to open bracket
    next if $skip;
    last if /\}/;

    print $_, "\n";

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.