simple regex for sed
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > simple regex for sed




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    simple regex for sed  
Oxnard


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-06 06: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









[ Post a follow-up to this message ]



    Re: simple regex for sed  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-06 06: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





[ Post a follow-up to this message ]



    Re: simple regex for sed  
Michael Tosch


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-06 12:29 PM

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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:20 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register