|
Home > Archive > Unix Shell > December 2006 > simple regex for sed
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 |
simple regex for sed
|
|
| Oxnard 2006-12-22, 1:33 am |
| What I am looking to do is suing sed come up with a way that if the value of
a variable is piped to sed and if the variable contains a non-number
character the whole thing becomes the word 'null'
something like:
t=999
echo $t | sed 's/[^0-9]/null/'
it allows the 999 to be printed which is great
but if I do
t=99u99k4
echo $t | sed 's/[^0-9]/null/'
I get 99null99k4
I really am looking for just the word 'null' to come out.
How can I do this
Thanks
| |
| Chris F.A. Johnson 2006-12-22, 1:33 am |
| On 2006-12-22, Oxnard wrote:
> What I am looking to do is suing sed come up with a way that if the value of
> a variable is piped to sed and if the variable contains a non-number
> character the whole thing becomes the word 'null'
>
> something like:
>
>
> t=999
>
> echo $t | sed 's/[^0-9]/null/'
>
> it allows the 999 to be printed which is great
>
> but if I do
>
> t=99u99k4
>
> echo $t | sed 's/[^0-9]/null/'
>
> I get 99null99k4
>
> I really am looking for just the word 'null' to come out.
>
> How can I do this
If you want to check a single variable (or even several), you don't
need sed. Save that for files.
case $t in
*[!0-9]*) echo null ;;
*) echo "$t" ;;
esac
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Michael Tosch 2006-12-22, 7:29 am |
| Oxnard wrote:
> What I am looking to do is suing sed come up with a way that if the value of
> a variable is piped to sed and if the variable contains a non-number
> character the whole thing becomes the word 'null'
>
> something like:
>
>
> t=999
>
> echo $t | sed 's/[^0-9]/null/'
>
> it allows the 999 to be printed which is great
>
> but if I do
>
> t=99u99k4
>
> echo $t | sed 's/[^0-9]/null/'
>
> I get 99null99k4
>
> I really am looking for just the word 'null' to come out.
>
As Chris pointed out, use shell expression.
With sed, you must change the regex to match the entire line,
so the entire line is replaced:
sed 's/.*[^0-9].*/null/'
--
Michael Tosch @ hp : com
|
|
|
|
|