Grep to ignore suffix & find end of line


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-22-2015
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
Code:
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.
Code:
          ADD XYZ TO AAA-BBB

Is there any way to fix this?
TIA
# 2  
Old 12-22-2015
I guess - as it is not too clear - that you want the second grepto match your file. Try the alternation (using | ) in a subexpression:
Code:
grep -E 'AAA-BBB([^-]|$)' file

(you may have to switch on "extended regexes")
This User Gave Thanks to RudiC For This Post:
wbport (12-22-2015)
# 3  
Old 12-22-2015
Using a $ anchor you can address all trailing characters
Code:
grep 'AAA[^-]*$' file

This User Gave Thanks to MadeInGermany For This Post:
wbport (12-22-2015)
# 4  
Old 12-22-2015
Quote:
Originally Posted by MadeInGermany
Using a $ anchor you can address all trailing characters
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.
Code:
    03  AAA    PIC 999V99.
             MULTIPLY AAA BY 100 GIVING OUT-AMT.
            ADD XYZ TO AAA

RudiC: Your suggestion works, many thanks!
# 5  
Old 12-23-2015
But does it fail on
Code:
ADD AAA TO AAA-BBB

A more reliable way is to create a cross reference listing in a compile.
# 6  
Old 12-23-2015
Would this do?
Code:
grep -E 'AAA(-BBB(-CCC)?)?([^-]|$)' file

# 7  
Old 12-23-2015
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.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Grep after - til the end of the line batchenr Shell Programming and Scripting 9 07-04-2017 05:44 AM
Need to add a numeric & special char to end of the first line Joselouis Shell Programming and Scripting 11 11-13-2016 01:36 AM
Grep command to ignore line starting with hyphen Srinraj Rao Shell Programming and Scripting 4 06-19-2015 02:36 AM
Grep start and end line of each segments in a file Veera_V Shell Programming and Scripting 2 09-05-2014 01:14 AM
Find keywords, and append at the end of line dtdt Shell Programming and Scripting 9 04-23-2014 12:56 PM
How to ignore comments at the end of the each line? sharsour Shell Programming and Scripting 4 11-12-2013 05:43 AM
Grep lines with numbers greater than 2 digits at the end of the line jimmyf UNIX for Dummies Questions & Answers 1 02-15-2013 02:37 AM
Using Grep & find & while read line in a script Abv_mx81 Shell Programming and Scripting 0 04-13-2012 01:25 PM
AWK-grep from line number to the end of file thibodc Shell Programming and Scripting 1 03-31-2012 11:55 PM
find a certain line and append text to the end of the line peachclift Shell Programming and Scripting 2 12-19-2011 04:10 PM
How to use grep & find command to find references to a particular file Gangam Shell Programming and Scripting 2 09-22-2011 04:52 AM
Find a string using grep & print the line above or below that. Zaib Shell Programming and Scripting 10 11-05-2010 09:51 AM
Grep from a starting line till the end of the file Mr. Zer0 Shell Programming and Scripting 3 09-13-2010 10:06 PM
a better way to grep until end of error message, although most seem to be 1 or 2 line VGR Shell Programming and Scripting 11 05-09-2009 03:41 PM
find & grep Anika UNIX for Dummies Questions & Answers 11 02-01-2001 12:19 PM