Operating System - HP-UX
1830930 Members
2429 Online
110017 Solutions
New Discussion

Regular Expression Question

 
SOLVED
Go to solution
Mihails Nikitins
Super Advisor

Regular Expression Question

Hi,

Please help me in composing very simple regular expression.

I'm parsing e-mail box and there is a need to distinguish two "From" lines

From root@host.com Fri Sep 6 18:32:25 2002
and
From: root@host.com

Both lines are retrieved by expression /From/
I'd like to get line with word 'From' followed by white space. The white space is my problem.

Thanks and points in advance!

BR,
Mihails


KISS - Keep It Simple Stupid
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: Regular Expression Question

Unde perl you would use-

/^From:?\s*(.*)/

Then $1 would contain text

-- Rod Hills
There be dragons...
Charles McCary
Valued Contributor

Re: Regular Expression Question

cat mail.box | grep "From "

or

Cat mail.box | grep From | grep -v "From:"

I think this is what you wanted?


c
Rodney Hills
Honored Contributor

Re: Regular Expression Question

How is white space being a problem?

grep "From " file

doesn't work?

-- Rod Hills
There be dragons...
Shannon Petry
Honored Contributor

Re: Regular Expression Question

I used grep relatively easily

grep "From\ "
will get the word "From" followed by a whitespace.
grep "From:" will grabe other
if the whitespace is a tab, then
grep "From\ "
where the whitespace above is a tab.

This should not require really a regular expression to get, but the same principles exist.


regards,
Shannon


Microsoft. When do you want a virus today?
Hai Nguyen_1
Honored Contributor

Re: Regular Expression Question

Mihails,

Try /From / (there is a space right behind From.)

Hai
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Regular Expression Question

/^From[^:]/

in most applications (awk, sed, perl

Charles, please unlearn 'cat':

grep /regex/
is both faster and more efficient than

cat file | grep /regex/

and as a bonus you'll hit maxproc less often :)
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Regular Expression Question

Hi:

Try:

awk '/From[ ]/ {print}'

Regards!

...JRF...
John Palmer
Honored Contributor

Re: Regular Expression Question

Try:
/From[|]/

Where is a single space character and a single tab.

Regards,
John
John Palmer
Honored Contributor

Re: Regular Expression Question

Another one is:

/From[:space:]/

See man 5 regexp

Regards,
John
Mihails Nikitins
Super Advisor

Re: Regular Expression Question

Thanks for your replies, I wanted nice regular expression with brackets, not grep. Even do not ask me why simplier solutions did not work. There was a mystic problem located between the seat and keyboard. :-)

BR,
Mihails
KISS - Keep It Simple Stupid