Unix Shell - grep returning only numbers from a string

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > grep returning only numbers from a string





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author grep returning only numbers from a string
Peter Sands

2004-04-22, 6:35 am

Hi,
I have a string containing a date:
$ echo $str_date
Thu Apr 22 10:55:05 BST 2004

I need to extract only the numbers ,( for further processing.)
So I end up with:
221055052004
Using grep I thought would do this:
$ echo $str_date | grep '[0-9]'
Thu Apr 22 10:55:05 BST 2004

But that just returns the whole string:

How do I go about doing this please,
thanks
PS
Brian Gough

2004-04-22, 6:35 am

peter_sands@techemail.com (Peter Sands) writes:

> Hi,
> I have a string containing a date:
> $ echo $str_date
> Thu Apr 22 10:55:05 BST 2004
>
> I need to extract only the numbers ,( for further processing.)
> So I end up with:
> 221055052004
> Using grep I thought would do this:
> $ echo $str_date | grep '[0-9]'
> Thu Apr 22 10:55:05 BST 2004
>
> But that just returns the whole string:
>
> How do I go about doing this please,
> thanks


The tr (translate) command can be used to delete ranges of characters,
e.g. tr -d -c '[0-9]' removes any characters not in the range 0-9

--
Brian Gough

Network Theory Ltd,
Publishing the GNU Bash Reference Manual --- http://www.network-theory.co.uk/
Chris F.A. Johnson

2004-04-22, 7:36 am

On Thu, 22 Apr 2004 at 09:59 GMT, Peter Sands wrote:
> Hi,
> I have a string containing a date:
> $ echo $str_date
> Thu Apr 22 10:55:05 BST 2004
>
> I need to extract only the numbers ,( for further processing.)
> So I end up with:
> 221055052004
> Using grep I thought would do this:
> $ echo $str_date | grep '[0-9]'
> Thu Apr 22 10:55:05 BST 2004
>
> But that just returns the whole string:


grep, execpt in recent GNU versions, only returns matching lines,
not partial lines.

> How do I go about doing this please,


If you are using bash2 or zsh, you can use parameter expansion:

echo ${str_date//[^0-9]/}

Or ksh93 (also works in bash2):

echo ${str_date//[!0-9]/}

If those are not an option, use tr:

echo "$str_date" | tr -dc '0-9'

Or sed:

echo "$q" | sed 's/[^0-9]//g'

Or awk:

echo "$q" | awk '{ gsub(/[^0-9]/,""); print}'


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Dan Mercer

2004-04-22, 10:44 am


"Peter Sands" <peter_sands@techemail.com> wrote in message news:6b009995.0404220159.49038351@posting.google.com...
: Hi,
: I have a string containing a date:
: $ echo $str_date
: Thu Apr 22 10:55:05 BST 2004

Are you getting this from the date command? If so, man date
to see how to get exactly the format you desire.

Dan Mercer

:
: I need to extract only the numbers ,( for further processing.)
: So I end up with:
: 221055052004
: Using grep I thought would do this:
: $ echo $str_date | grep '[0-9]'
: Thu Apr 22 10:55:05 BST 2004
:
: But that just returns the whole string:
:
: How do I go about doing this please,
: thanks
: PS


Kevin Collins

2004-04-22, 4:35 pm

In article <6b009995.0404220159.49038351@posting.google.com>, Peter Sands wrote:
> Hi,
> I have a string containing a date:
> $ echo $str_date
> Thu Apr 22 10:55:05 BST 2004
>
> I need to extract only the numbers ,( for further processing.)
> So I end up with:
> 221055052004
> Using grep I thought would do this:
> $ echo $str_date | grep '[0-9]'
> Thu Apr 22 10:55:05 BST 2004
>
> But that just returns the whole string:
>
> How do I go about doing this please,
> thanks
> PS


Peter,

the other folks have basically given you good answers to
your question. However, if you end up just translating out the
non-digits, beware of any case where the timezone might contain
a number. I noticed your TZ is BST, but mine happens to be
PST8PDT, which would throw a wrench into your scheme.

Dan has the best idea, if you are generating the date...

Kevin
Walt R

2004-04-22, 5:34 pm

"Dan Mercer" <dmercer@mn.rr.com> wrote in message news:<2RQhc.29020$lS2.3955@twister.rdc-kc.rr.com>...
> "Peter Sands" <peter_sands@techemail.com> wrote in message news:6b009995.0404220159.49038351@posting.google.com...
> : Hi,
> : I have a string containing a date:
> : $ echo $str_date
> : Thu Apr 22 10:55:05 BST 2004
>
> Are you getting this from the date command? If so, man date
> to see how to get exactly the format you desire.
>
> Dan Mercer
>
> :
> : I need to extract only the numbers ,( for further processing.)
> : So I end up with:
> : 221055052004
> : Using grep I thought would do this:
> : $ echo $str_date | grep '[0-9]'
> : Thu Apr 22 10:55:05 BST 2004
> :
> : But that just returns the whole string:
> :
> : How do I go about doing this please,
> : thanks
> : PS


**
If you do not use 24 hour time, you can not distinguish between AM & PM.

There are many ways to do this. The quickest for me is to use sed
to substitute a space for the colons, and pipe it to awk to print
the desired strings. Date can output in many formats.

Is this a homework assignment?

Walt R.
Stephane CHAZELAS

2004-04-23, 3:34 am

2004-04-22, 20:27(+00), Kevin Collins:
[...]
[...][vbcol=seagreen]
> the other folks have basically given you good answers to
> your question. However, if you end up just translating out the
> non-digits, beware of any case where the timezone might contain
> a number. I noticed your TZ is BST, but mine happens to be
> PST8PDT, which would throw a wrench into your scheme.

[...]

But, in that case, date would not output PST8PDT:

~$ TZ=PST8PDT date
Fri Apr 23 00:14:26 PDT 2004

because it just reports the _name_ of the _current_ time zone

--
Stéphane ["Stephane.Chazelas" at "free.fr"]
Kevin Collins

2004-04-26, 7:38 pm

In article <slrnc8hh01.5l.stephane.chazelas@spam.is.invalid>, Stephane CHAZELAS
wrote:
> 2004-04-22, 20:27(+00), Kevin Collins: [...]
> [...]
> [...]
>
> But, in that case, date would not output PST8PDT:
>
> ~$ TZ=PST8PDT date Fri Apr 23 00:14:26 PDT 2004
>
> because it just reports the _name_ of the _current_ time zone


I stand corrected - thanks for catching that! I guess I need more caffeine
before I post

Kevin
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com