Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Grep to ignore suffix & find end of line


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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  
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:
# 3  
Using a $ anchor you can address all trailing characters
Code:
grep 'AAA[^-]*$' file

This User Gave Thanks to MadeInGermany For This Post:
# 4  
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  
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  
Would this do?
Code:
grep -E 'AAA(-BBB(-CCC)?)?([^-]|$)' file

# 7  
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 command to ignore line starting with hyphen
Srinraj Rao
Hi, I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file. for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'` do . . done How...... Shell Programming and Scripting
4
Shell Programming and Scripting
How to ignore comments at the end of the each line?
sharsour
Hi All, I am reading the host file by ignoring the comments and write it to the other file. I am reading with regular expression for IP address. grep -E '^{1,3}\.{1,3}\.{1,3}\.{1,3}' $inputFile | awk '{for(i=2;i<=NF;i++)print $1,$i}' > $DR_HOME/OS/temp After that am reading each host...... Shell Programming and Scripting
4
Shell Programming and Scripting
Using Grep & find & while read line in a script
Abv_mx81
Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one...... Shell Programming and Scripting
0
Shell Programming and Scripting
How to use grep & find command to find references to a particular file
Gangam
Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command .... Shell Programming and Scripting
2
Shell Programming and Scripting
Find a string using grep & print the line above or below that.
Zaib
Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris.... Shell Programming and Scripting
10
Shell Programming and Scripting

Featured Tech Videos