Regex negative look and bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex negative look and bash script
# 1  
Old 04-27-2017
Regex negative look and bash script

My script have to read logfile, and take some action, if in pattern are strings:
1) exit 0 strings pattern
... "INF - Status"... success

2) exit 1 (! as not) strings pattern
... "INF - Status"... !success
Simple example, what works

Code:
#!/bin/bash
tail -f regex.log | while read LOGLINE
        do
        if [[ $LOGLINE =~ .*INF\ -\ Status.*success.* ]]
        then
        date "+%d.%B.%Y %H:%M:%S"
        echo "$LOGLINE"
        TAILKILL=$(pgrep -P $$ -x tail)
        kill -9 $TAILKILL
fi
done

But i can't figure out how to write a trap where string "success" does not exists while string "INF - Status" must be. regex negative look.

Last edited by kvaikla; 04-27-2017 at 03:41 PM..
# 2  
Old 04-27-2017
Use code tags for code please.

Why not two regexes?
Code:
if [[ CONDITION1 ]] && ! [[ CONDITION2 ]]

Presumably that kill -9 is killing tail. Don't bother. Just break out of the loop. This will close the pipe and implicitly kill tail with SIGPIPE, because pipes are designed to clean up after themselves like that. Much simpler.

Code:
tail -f regex.log | while read LOGLINE
        do
        if [[ $LOGLINE =~ .*INF\ -\ Status.*success.* ]] && ! [[ $LOGLINE =~ .*success.* ]]
        then
                date "+%d.%B.%Y %H:%M:%S"
                echo "$LOGLINE"
                break
        fi
done

# 3  
Old 04-27-2017
Thnks
actually
Code:
...
if [[ $LOGLINE =~ .*INF\ -\ Status.* ]] && ! [[ $LOGLINE =~ .*success.* ]]
...

this way.
It means I can't do a negative look inside [[...]] but only using a ![[..]] ?
br
Kaido
# 4  
Old 04-27-2017
That's not how a traditional regex works, you describe what you want, not what you don't want.

Only thing I can think is PERL regexes which have all sorts of fancy extra features which may include that, but this is not PERL.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-27-2017
No, you cannot do that in bash. If you want to ensure that success is only searched at the end then you have to repeat the pattern.
Code:
pat="INF - Status"
tail -f regex.log |
while read LOGLINE
do
  if [[ $LOGLINE =~ $pat ]] && [[ ! $LOGLINE =~ $pat.*success ]]
  then

BTW a break does not terminate the tail -f in most shells. In Solaris only the old /bin/sh works like this.
# 6  
Old 04-28-2017
Quote:
Originally Posted by kvaikla
Thnks
actually
Code:
...
if [[ $LOGLINE =~ .*INF\ -\ Status.* ]] && ! [[ $LOGLINE =~ .*success.* ]]
...

this way.
It means I can't do a negative look inside [[...]] but only using a ![[..]] ?
br
Kaido
Yes you can do that as well:
Code:
if [[ $LOGLINE =~ .*"INF - Status".* && ! $LOGLINE =~ .*success.* ]]

which is equivalent to:
Code:
if [[ $LOGLINE =~ "INF - Status" && ! $LOGLINE =~ success ]]

# 7  
Old 04-28-2017
Quote:
Originally Posted by MadeInGermany
No, you cannot do that in bash. If you want to ensure that success is only searched at the end then you have to repeat the pattern.
Code:
pat="INF - Status"
tail -f regex.log |
while read LOGLINE
do
  if [[ $LOGLINE =~ $pat ]] && [[ ! $LOGLINE =~ $pat.*success ]]
  then

BTW a break does not terminate the tail -f in most shells. In Solaris only the old /bin/sh works like this.
Why not? Does it hang instead?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like: #!/bin/bash SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'" java -cp /my/class/path MyClassName $SEARCH... (3 Replies)
Discussion started by: stonkers
3 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. UNIX for Dummies Questions & Answers

Regex for (a|b) in bash

I am trying to find files using the following by using simple bash script: if -2014 ]]; then echo "yes";fi What I need to find are any files with date 08-**-2014 so August 2014 any files. I can use if -2014 ]]; then echo "yes";fi That works fine. How do I get files beginning with 08... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

5. Shell Programming and Scripting

Is it possible to Divide a negative number in bash script

I am using a small script to divide some numbers in a given file and display the output in another file. I am getting the following error basename: invalid option -- '5' Try `basename --help' for more information. (standard_in) 1: syntax error The script is : #!/bin/bash for i in `cat... (4 Replies)
Discussion started by: kmnr877
4 Replies

6. UNIX for Dummies Questions & Answers

Need help with Regex for bash

Hi, I am trying to match this word: hexagon-bx.mydomain.com with regex. I have tried this: "\.*]*$" So far I have not been successful. I also need to make sure that the regex will match words that just have lowercase letters and numbers in them, such as camera01. How can I create such an... (5 Replies)
Discussion started by: newbie2010
5 Replies

7. Shell Programming and Scripting

Bash regex help

I've been using the following regex below in a bash script on RHEL 5.5 using version GNU bash, version 3.2.25(1)-release I've tried using the script on RHEL 6.3 which uses GNU bash, version 4.1.2(1)-release I assume there's been alot of changes to bash since that's quite a jump in revisions.... (12 Replies)
Discussion started by: woodson2
12 Replies

8. Shell Programming and Scripting

[BASH] Allow name with spaces (regex)

Hey all, I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them) isAlpha='^*$'However, when I bring this over to BASH it doesn't allow me to enter spaces. I use the... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

9. Shell Programming and Scripting

Bash regex

Hello everybody, I'm clearly not an expert in bash scripting as I've written maybe less than 10 scripts in my life. I'm trying to strip an xml string removing every tag in it. I'm using bash substitution to do so, but apparently I missed something about what is a regex for bash ... As an... (4 Replies)
Discussion started by: kerloi
4 Replies

10. Shell Programming and Scripting

printf in bash shell not printing negative values

hi i am using printf in a script and it is not printing negative values..i have to use printf to get rid of the newline..here is my code: fin=`echo $a - $b | bc` printf "${fin}," >> test these statements are in a loop. here is what i get when i try to subtract 4 from 8: ./scr1: line... (2 Replies)
Discussion started by: npatwardhan
2 Replies
Login or Register to Ask a Question