Unix/Linux Go Back    


SCO Santa Cruz Operation (SCO) was a software company based in Santa Cruz, California which was best known for selling three UNIX variants for Intel x86.

Grep to ignore suffix & find end of line

SCO


Tags
grep, regular expression anchoring, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-22-2015
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 27 March 2017, 5:01 PM EDT
Location: Mississippi
Posts: 91
Thanks: 29
Thanked 13 Times in 13 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 12-22-2015
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 March 2017, 3:59 PM EDT
Location: Aachen, Germany
Posts: 10,552
Thanks: 256
Thanked 3,233 Times in 2,980 Posts
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")
The Following User Says Thank You to RudiC For This Useful Post:
wbport (12-22-2015)
Sponsored Links
    #3  
Old Unix and Linux 12-22-2015
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 28 March 2017, 3:01 PM EDT
Location: Simplicity
Posts: 3,462
Thanks: 267
Thanked 1,141 Times in 1,036 Posts
Using a $ anchor you can address all trailing characters

Code:
grep 'AAA[^-]*$' file

The Following User Says Thank You to MadeInGermany For This Useful Post:
wbport (12-22-2015)
    #4  
Old Unix and Linux 12-22-2015
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 27 March 2017, 5:01 PM EDT
Location: Mississippi
Posts: 91
Thanks: 29
Thanked 13 Times in 13 Posts
Quote:
Originally Posted by MadeInGermany View Post
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!
Sponsored Links
    #5  
Old Unix and Linux 12-23-2015
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 26 March 2017, 7:20 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,930
Thanks: 2
Thanked 250 Times in 236 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 12-23-2015
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 March 2017, 3:59 PM EDT
Location: Aachen, Germany
Posts: 10,552
Thanks: 256
Thanked 3,233 Times in 2,980 Posts
Would this do?
Code:
grep -E 'AAA(-BBB(-CCC)?)?([^-]|$)' file

Sponsored Links
    #7  
Old Unix and Linux 12-23-2015
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 27 March 2017, 5:01 PM EDT
Location: Mississippi
Posts: 91
Thanks: 29
Thanked 13 Times in 13 Posts
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.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep command to ignore line starting with hyphen Srinraj Rao Shell Programming and Scripting 4 06-19-2015 01:36 AM
How to ignore comments at the end of the each line? sharsour Shell Programming and Scripting 4 11-12-2013 04:43 AM
Using Grep & find & while read line in a script Abv_mx81 Shell Programming and Scripting 0 04-13-2012 12:25 PM
find a certain line and append text to the end of the line peachclift Shell Programming and Scripting 2 12-19-2011 03:10 PM
Find a string using grep & print the line above or below that. Zaib Shell Programming and Scripting 10 11-05-2010 08:51 AM



All times are GMT -4. The time now is 05:43 PM.