Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC results mixed in with just AAA. My try is to ignore the field if it ends with a final hyphen so I wouldn't see "AAA-".

I have tried

grep 'AAA[^-]' progname
grep 'AAA-BBB[^-]' progname
grep 'AAA-BBB-CCC[^-]' progname

in a script.
The problem was that grep could not find a field if it was on the end of a line.

          ADD XYZ TO AAA-BBB

Is there any way to fix this?
TIA

I guess - as it is not too clear - that you want the second grep to match your file. Try the alternation (using | ) in a subexpression:

grep -E 'AAA-BBB([^-]|$)' file

(you may have to switch on "extended regexes")

1 Like

Using a $ anchor you can address all trailing characters

grep 'AAA[^-]*$' file
1 Like

Many thanks, that did the trick!

---------- Post updated at 01:54 PM ---------- Previous update was at 01:22 PM ----------

On second thought, almost. If there is a hyphen anywhere later on the line, the line is thrown out. The first line of this code is OK, the second isn't, and the third is ok.

    03  AAA    PIC 999V99.
             MULTIPLY AAA BY 100 GIVING OUT-AMT.
            ADD XYZ TO AAA

RudiC: Your suggestion works, many thanks!

But does it fail on

ADD AAA TO AAA-BBB

A more reliable way is to create a cross reference listing in a compile.

Would this do?

grep -E 'AAA(-BBB(-CCC)?)?([^-]|$)' file

Every field in a work area was supposed to be added to when certain filters were true and, in a control break, non zero values of those fields would trigger further processing. I had started by copying that work area to a new file, removing everything but the data names, then building identical grep commands around it. The suggestion RudiC made helped me check out and document that program as well as giving me a useful tool for the future.

I've used other compilers that produce sorted cross references of all data names but the compiler I use now doesn't do that.

The GNUCobol compiler can produce a cross reference. It supports most dialects, so you might get a clean compile.