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   -   Original Discussion by wbport
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 15 November 2017, 2:47 PM EST
Location: Mississippi
Posts: 121
Thanks: 34
Thanked 18 Times in 18 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   -   Original Discussion by wbport
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,618 Times in 3,323 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   -   Original Discussion by wbport
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 November 2017, 3:15 PM EST
Location: Simplicity
Posts: 3,828
Thanks: 319
Thanked 1,283 Times in 1,161 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   -   Original Discussion by wbport
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 15 November 2017, 2:47 PM EST
Location: Mississippi
Posts: 121
Thanks: 34
Thanked 18 Times in 18 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   -   Original Discussion by wbport
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 22 November 2017, 8:00 AM EST
Location: 44.21.48N 80.50.15W
Posts: 2,002
Thanks: 2
Thanked 272 Times in 255 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   -   Original Discussion by wbport
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,618 Times in 3,323 Posts
Would this do?
Code:
grep -E 'AAA(-BBB(-CCC)?)?([^-]|$)' file

Sponsored Links
    #7  
Old Unix and Linux 12-23-2015   -   Original Discussion by wbport
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 15 November 2017, 2:47 PM EST
Location: Mississippi
Posts: 121
Thanks: 34
Thanked 18 Times in 18 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 02:36 AM
How to ignore comments at the end of the each line? sharsour Shell Programming and Scripting 4 11-12-2013 05:43 AM
Using Grep & find & while read line in a script Abv_mx81 Shell Programming and Scripting 0 04-13-2012 01:25 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
Find a string using grep & print the line above or below that. Zaib Shell Programming and Scripting 10 11-05-2010 09:51 AM



All times are GMT -4. The time now is 04:21 PM.