Simple regexp doesn't work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple regexp doesn't work
# 1  
Old 06-08-2010
Simple regexp doesn't work

I'm pretty experienced with regexps, but I just can't get this expression to work.

The first line of my test file is this:

**** info Fri Jun 04 12:37:58 PDT 2010 stuff

I'm piping this file into this:

egrep '^****\s+[a-z]+[\t ]*Fri Jun 04'

This returns 0 lines.

If I change the expression to "^****\s+[a-z]+[\t ]*", it does find lines, but I need to be able to specify the date string.

I must be missing something simple, but I just don't see it.
# 2  
Old 06-08-2010
Code:
egrep '^\*\*\*\*\s+[a-z]+[\t ]*Fri Jun 04'

maybe? :P
# 3  
Old 06-08-2010
* is a special character in regular expressions, meaning 'zero or more of the previous character'. So the regex "a*" would match an empty string, a, aa, aaaaaaaaaaaaa etc. To match a literal * you need to escape them.

Code:
# This matches other things since * are length specifiers, not literals
$ echo "que" | egrep "^***"
que
# this rejects the string 'que' like it should
$ echo "que" | egrep "^\*\*\*"
# ...and matches *** like it should
$ echo "***" | egrep "^\*\*\*"
***
$

# 4  
Old 06-08-2010
Yeah, missing the "\*" was a stupid mistake. It's still not working, however.

If I use '^\*\*\*\*\s*[a-z]*\s*', it finds lines, but not '^\*\*\*\*\s*[a-z]*\s*Fri Jun'.

If I change it to '^\*\*\*\*\s+[a-z]*\s*' or '^\*\*\*\*\s*[a-z]+\s*', it also doesn't find any lines.
# 5  
Old 06-08-2010
use [[:blank:]] instead of \s, couse egrep doesn't support \s:
Code:
egrep '^\*\*\*\*[[:blank:]]+[a-z]+[[:blank:]]+Fri Jun 04'

# 6  
Old 06-08-2010
I'm on Solaris 10, if that matters, but my egrep appears to be different from what you have.

Character classes are recognized as "[:blank:]", not "[[:blank:]]" (according to the regex(5) man page). In any case, it has no trouble recognizing "\s". I get the same results with "\s*" as I get with "[:blank:]*". I get no results with "[[:blank:]]".

To recap, I get lines with '^\*\*\*\*\s*[a-z]*\s*' and '^\*\*\*\*[:blank:]*[a-z]*[:blank:]*', but not '^\*\*\*\*[:blank:]+[a-z]+[:blank:]+', '^\*\*\*\*[:blank:]+[a-z]+[:blank:]+Fri', '^\*\*\*\*[:blank:]*[a-z]*[:blank:]*Fri', or '^\*\*\*\*\s*[a-z]*\s*Fri'.

There appear to be two issues here. One I can work around, and the other I cannot. For some reason, using "+" instead of "*" never matches. In my case, I can use "*" if I can't get "+" to work. The other issue is that adding "Fri" to the end of a working expression makes it find no lines.
# 7  
Old 06-08-2010
try:
Code:
/usr/gnu/bin/egrep '^\*\*\*\*[[:blank:]]+[a-z]+[[:blank:]]+Fri Jun 04'

or
Code:
/usr/xpg4/bin/egrep '^\*\*\*\*[[:blank:]]+[a-z]+[[:blank:]]+Fri Jun 04'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

2. HP-UX

Sudo doesn't work

I edited sudoers like this:vi /etc/sudoers subex ALL =(root) NOPASSWD: /usr/ccs/bin/pstack But the respective user still is prompted for password, and even when the right password is used, the command is still not launched.$sudo usr/ccs/bin/pstack 26557 We trust you have received the usual... (5 Replies)
Discussion started by: black_fender
5 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Solaris

shutdown -y -i5 -g0 DOESN'T work

hello, The command above seems not working on my solaris 8/9 sparc machines. a. resulted to the ff below when I instead use "shutdown" only. Broadcast Message from root (pts/1) on "hostname" date.. The system "hostname" will be shut down in 30 seconds THE SYSTEM bdosg IS BEING SHUT... (4 Replies)
Discussion started by: lhareigh890
4 Replies

5. Shell Programming and Scripting

Lipo doesn't work

Hi guys, Am using lipo to merge ppc and i386 version of a static/dylib file based on "file type to load". I am working on Mac OS 10.5.6 and new to shell scripting. Please help me out. This is my code. echo "This file combine ppc and i386 file to form universal library" echo "source... (4 Replies)
Discussion started by: vishwesh
4 Replies

6. Shell Programming and Scripting

if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work

Hallo, I need to test a String (a special ip number-string). So I want to run that: ipadress=172.0.0.0 # for debugging: echo $ipadress | egrep -e '172\.?\.??\.??$' # the test that doesnt work if test -z `echo $ipadress | egrep -e '172\.?\.??\.??$'` != "" then echo "match" else... (1 Reply)
Discussion started by: wiseguy
1 Replies

7. Shell Programming and Scripting

awk -v -- Why doesn't my example work?

Hi. I've been playing around a bit. This isn't for any practical purpose-- it's really just a theoretical exercise. I wrote this little thing: foreach num ( 6 5 4 ) awk -v "number=$num" 'BEGIN{for(x=0;x<$number;x++) printf "-"; printf "\n"}' end I would expect the following output: ... (3 Replies)
Discussion started by: treesloth
3 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

lazy capture don't work (regexp in perl)

hello all im trying to capture only the first brackets but no matter what i do i keep capturing from the first brackets to the last one , here what i have : <% if (!Env.strLen(oidInvoice)); szDocumentTitle = Env.formatS("S",Env.getMsg("BP_INVOICE_ENUMERATION_CREATE_TITLE")) %> and... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question