Register
It is currently Sat Apr 19, 2014 11:02 am

Regular Expressions


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Jan 12, 2006 1:03 am   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
I have have a line of text which contains a 24hour time imbeded in it. Something like:

"It is now 2130 its time to begin"

The time can be anywhere in the string.

How can I strip out just the four digits that make up the time? I'm reading some stuff of regular expressions, and [0-9][0-9][0-9][0-9] is the proper pattern, but how can I use that to get the specific numbers out of the line? I'm just not getting how to use the expressions. Thanks.


Top
 Profile  
 PostPosted: Thu Jan 12, 2006 2:04 am   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
doh, its grep -o I can't ever find anything in a man page until I read it 20 times...........


Top
 Profile  
 PostPosted: Fri Jan 13, 2006 8:46 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
lol... i'm right there with you. I think that they should convert manpages to pdf... that way the flags can be indexed... and you could just jump to them in the order you want, and by pass all the extra crud :)


Top
 Profile  
 PostPosted: Thu Oct 30, 2008 6:00 am   

Joined: Wed Sep 24, 2008 11:32 pm
Posts: 7
Hi, guys, I didn't find a solution with grep, but found workrounds with tr and sed. May these help.

First I want to make it clear that while [0-9][0-9][0-9][0-9] works quite well, [0-9]{4} do the same job.

As in the "tr" case, I tried
Code:
echo "It is now 2130 its time to begin" | tr -d [[:alpha:]] | tr -d " "

It works.
I have been learning the powerfull sed these days, so I gave it a try:
Code:
echo "It is now 2130 its time to begin" | sed 's/ /\n/g' | grep -E [0-9]{4}

The sed command here replace whitespace with a ...... hmmm, line terminator ( Sorry, I am not sure about the right jargon. lol ), thus place every word in a seperate line.
BTW, I found the first line of grep manual is " grep , egrep, fgrep, rgrep, - print lines matching a pattern". So maybe it print "lines" instead of specified words in lines in my humble opion.


Top
 Profile  
 PostPosted: Fri Oct 31, 2008 6:12 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Slightly more simple than the above

Code:
echo "It is now 2130 its time to begin" | sed 's/^.*\([[:digit:]][[:digit:]][[:digit:]][[:digit:]]\).*$/\1/'


irritatingly I cannot seem to persuade sed to use the [[:digit:]]{4} syntax :(


Top
 Profile  
 PostPosted: Mon Nov 17, 2008 9:25 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
DarthWavy wrote:
Slightly more simple than the above

Code:
echo "It is now 2130 its time to begin" | sed 's/^.*\([[:digit:]][[:digit:]][[:digit:]][[:digit:]]\).*$/\1/'


irritatingly I cannot seem to persuade sed to use the [[:digit:]]{4} syntax :(


Why not just use this?
Code:
echo "It is now 2130 its time to begin" | sed "s/^.*\([0-9]\{4\}\).*$/\1/"

Anyway, I think the problem is that you haven't escaped {}.
http://www.ibm.com/developerworks/aix/l ... xp/#list16 there is a list of the simple things that needs to be escaped to use.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP