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 ]
|