Common Expressions for Numbers in BASH

Introduction

It is a quick put up on methods to acknowledge numbers akin to easy integers, actual numbers and particular codes akin to zip codes and bank card numbers and in addition extract these quantity from unstructured textual content within the in style bash (Bourne Once more Shell) shell or scripting language. Bash is the default Unix console or terminal window on Macintosh computer systems in addition to various different Apple merchandise. It is usually utilized by cygwin and several other different variants of Unix and Linux.

Common expressions are a compact environment friendly approach of representing patterns of characters together with the letters within the English alphabet and digits. There may be in depth info on the net on common expression. readers can begin with the Wikipedia web page on common expressions. The aim of this put up in for example particularly methods to acknowledge widespread kinds of numbers utilizing common expressions in BASH. The common expressions within the examples can even work in another environments.

The instance scripts beneath had been examined on a MacBook Air with Mac OS X 10.10.3 (Yosemite) and this model of the bash shell:

GNU bash, model 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software program Basis, Inc.

NOTE: In case you are new to programming or common expressions, common expressions will not be derived from the English language or fundamental arithmetic taught in colleges. Usually features of programming that aren’t near English or commonplace arithmetic are more durable to grasp and require extra follow, drilling, and subsequent continued use to grasp. Programming languages akin to BASIC or Python which can be nearer to English are simpler to be taught and grasp than languages akin to C or C++ that make heavy use of customized pc notations and phrases not present in commonplace English or commonplace college arithmetic. Common expressions are approach on the market within the cryptic pc notation wilderness.

The instance bash scripts are listed beneath. They’re additionally accessible at GitHub. The HTTPS URL for the Git repository at GitHub is:

https://github.com/jmcgowan79/mathbash.git

To get it’s essential to have Git put in and configured in your pc. Then:

$ git clone https://github.com/jmcgowan79/mathbash.git
$ cd mathbash
$ chmod ugo+x *.sh             # make scripts executable
mathbash$ ./test_isnumber.sh   # to check the set up

Common Expressions for Numbers

This script acknowledges varied kinds of numbers together with integers, actual numbers, complicated numbers, and particular codes together with zip codes, phone numbers, and bank card numbers.

isnumber.sh

#!/bin/bash
# take a look at if a string is a quantity, report sort of the quantity (e.g. INTEGER, REAL, and so on.)
# exit code is 0 for achievement -- string is a NUMBER
# exit code is 1 for failure -- string is NOT A NUMBER
#
# illustrates common expressions for recognizing quantity strings
#
# bash is typically in /usr/native/bin/bash
#
# (C) 2015 John F. McGowan, Ph.D. 

if [[ "$#" -ne 1 || "$1" == "-h" || "$1" == "-?" ]]; then
    echo "Utilization: `basename $0`  "
    echo "  -- experiences quantity sort of possible_number_string "
    echo "  -- POSITIVE INTEGER, NON-NEGATIVE INTEGER, SIGNED INTEGER"
    echo "  -- HEXADECIMAL, REAL NUMBER, VECTOR, ZIP CODE, TELEPHONE NUMBER "
    echo "  -- CREDIT CARD NUMBER"
    echo " "
    echo "  -- ILLUSTRATES REGULAR EXPRESSIONS FOR RECOGNIZING NUMBER STRINGS"
    echo " "
    echo '  -- use bash$ echo $? to check exit code'
    echo "  -- use enclosing quotes for string with areas akin to bank card numbers"
    echo " "
    echo " Creator: John F. McGowan, Ph.D. ([email protected])"
    echo " "
   exit 0
fi

# Unix/bash exit code of 0 means success (is a quantity on this case)
is_number=1  # begin no quantity discovered

number_string=$1

# common expressions match patterns of characters
#
# caret ^ represents the beginning of a string or line exterior of brackets
# greenback $ represents the tip of a string or line
# sq. brackets [1-9] characterize all characters within the brackets
# [abc] for instance might be "a," "b," or "c"
# hyphen inside brackets signifies a spread of characters
# usually digits or letters
# [1-9] represents the digits in vary 1,2,3,...9
# [a-c] represents the letters a,b,c
#
# inside brackets caret ^ negates the checklist of characters
# for instance [^0-9] represents all characters EXCEPT 0,1,2,...9
#
# ? signifies 0 or 1 of previous sample
# * signifies 0 or extra of previous sample
# + signifies 1 or extra of previous sample
# . matches any single character
#
# (...) is a bunch
# for instance, (ab)? matches (nothing) or ab
# for instance, (ab)* matches (nothing), ab, abab, ...
# for instance, (ab)+ matches ab, abab, ababab, ...
# (...){n,m} signifies from n to m repetitions of the sample
# for instance, (ab){2,3} matches solely abab and ababab
# the backlash is used to flee the characters with particular meanings
# ^ $ ( ) [ ] { } * . ? +
#
# =~ is the reguar expression sample matching operator in bash

# constructive integers/counting numbers (1,2,3,...)
if [[ $number_string =~ ^[1-9][0-9]*$ ]]; then
    echo "POSITIVE INTEGER"
    is_number=0
fi

# add zero to numbers
# zero was remarkably tough to invent
# the traditional Babylonians had a place-value
# quantity system primarily based on 60 (not 10) which
# included an implicit zero, however the express
# image for zero took many extra centuries to
# invent

# non-negative integers (0,1,2,...)
if [[ $number_string =~ ^[0-9]+$ ]]; then
    echo "NON-NEGATIVE INTEGER"
    is_number=0
fi

# unfavorable numbers are even much less apparent

# signed integers (..., -2, -1, 0, 1, 2,...)
if [[ $number_string =~ ^[+-][0-9]+$ ]]; then
    echo "SIGNED INTEGER"
    is_number=0
fi

# hexadecimal numbers are used with computer systems
# and low-level programming of computer systems

# hexadecimal (base 16) numbers akin to AA12 or 0x12ab and so on.
if [[ $number_string =~ ^(0[xX])?[0-9a-fA-F]+$ ]]; then
    # additionally acknowledge C format hex numbers akin to 0xaf12
    echo "HEXADECIMAL NUMBER (INTEGER)"
    is_number=0
fi

# fractions akin to 1/2, 1/3 date to antiquity however the
# idea of actual numbers akin to sq. root of two
# proved tough to understand.  The traditional Greeks
# knew a proof that the sq. root of two couldn't
# be a ratio of two integers, however had been apparently
# unable to make the leap to actual numbers.

# actual numbers/decimal numbers (0.0, ..., 0.5, ..., 1.0, ..., 3.1415...,...)
real_regexp="[+-]?([0-9]+|[0-9]+.[0-9]*|.[0-9]+)"
if [[ $number_string =~ ^$real_regexp$ ]]; then
    echo "REAL NUMBER"
    is_number=0
fi

# vectors are normally used to characterize a magnitude
# with a course such because the course and velocity
# of the wind or an ocean present (early makes use of of
# the vector idea)

# vector with enclosing parenthesis, e.g. (1, 2, 3)
vector_regexp="(( *$real_regexp, *)+$real_regexp *)"
if [[ $number_string =~  ^$vector_regexp$ ]]; then
    echo "VECTOR"
    is_number=0
fi

# vector with enclosing brackets, e.g. [1, 2, 3]
vector_regexp="[( *$real_regexp, *)+$real_regexp *]"
if [[ $number_string =~  ^$vector_regexp$ ]]; then
    echo "VECTOR"
    is_number=0
fi

# vector with enclosing curly braces, e.g {1, 2, 3}
vector_regexp="{( *$real_regexp, *)+$real_regexp *}"
if [[ $number_string =~  ^$vector_regexp$ ]]; then
    echo "VECTOR"
    is_number=0
fi

# the imaginary numbers turned up in roots of
# polynomials and at the moment are utilized in everthing from
# electrical engineering, cryptography, to
# quantum mechanics, however stay mysterious.

# pure imaginary numbers i = sq. root(-1)
if [[ $number_string =~ ^$real_regexp[iI]$ ]]; then
    echo "PURE IMAGINARY NUMBER";
    is_number=0
fi

# complicated numbers (1.1 + 2i, -1 + 2.1i, ...)
#
complex_regexp="$real_regexp( *[+-] *($real_regexp)?[iI])?"
if [[ $number_string =~ ^$complex_regexp$ ]]; then
    echo "COMPLEX NUMBER"
    is_number=0
fi

# giant integers are regularly used as distinctive identifiers

# zip code (United States)
if [[ $number_string =~ ^[0-9]{5,5}(-[0-9]{4,4})?$ ]]; then
    echo "ZIP CODE (USA)"
    is_number=0
fi

# phone quantity (USA)

if [[ $number_string =~ ^(([0-9]( |-))?[0-9]{3,3} +|([0-9]( |-))?([0-9]{3,3}) *)?[0-9]{3,3}( |-)[0-9]{4,4}$ ]]; then
    echo "TELEPHONE NUMBER (USA)"
    is_number=0
fi

# bank card quantity  (16 digits)

if [[ $number_string =~ ^[0-9]{16,16}|([0-9]{4,4} ?){4,4}$ ]]; then
    echo "CREDIT CARD NUMBER"
    # take away areas from bank card quantity
    number_string_cleaned=${number_string// /}
#    echo ""
    if [[ $number_string_cleaned =~ ^4[0-9]{6,}$ ]]; then
	echo "PROBABLE VISA CARD (VISA CARD START WITH 4)";
    fi
    if [[ $number_string_cleaned =~ ^5[1-5][0-9]{5,}$ ]]; then
	echo "PROBABLE MASTER CARD";
    fi
    is_number=0
fi

# report if string is just not a quantity
#
if [[ $is_number == 1 ]]; then
    echo "NOT A NUMBER"
fi

exit $is_number


How one can Extract Numbers from Textual content utilizing Common Expressions in BASH

This quick script exhibit methods to use the common expressions for numbers to extract numbers and numeric knowledge from unstructured textual content, a standard drawback on this age of the Web. Observe that bash shops the primary matched sub-pattern indicated by enclosing parenthesis within the common expression within the particular variable BASH_REMATCH[1]

extract_number.sh

#!/bin/bash
#
# instance of extracting quantity from textual content utilizing common expressions in bash
# -- we regularly need to extract numerical knowledge from unstructured textual content
#
# illustrates common expressions for recognizing quantity strings
#
# bash is typically in /usr/native/bin/bash
#
# (C) 2015 John F. McGowan, Ph.D. ([email protected])


if [[ "$1" == "-h" || "$1" == "-?" ]]; then
    echo "Utilization: `basename $0`  extract quantity from string "
    echo "       -- exit code 1 if no quantity discovered"
    echo "       -- exit code 0 if a quantity is discovered"
    echo "       -- experiences quantity if discovered"
    echo " "
    echo " Creator: John F. McGowan, Ph.D. ([email protected])"
    echo " "
    exit 0
fi

found_number=1  # have not discovered quantity but
#
# inside brackets, caret negates the checklist of characters
# [^0-9] matches all characters apart from 0,1,2...9
#
# actual numbers/decimal numbers (0.0, ..., 0.5, ..., 1.0, ..., 3.1415...,...)
real_regexp="[+-]?([0-9]+|[0-9]+.[0-9]*|.[0-9]+)"
complex_regexp="$real_regexp( *[+-] *($real_regexp)?[iI])?"

if [[ $1 =~ [^0-9-]*($complex_regexp) ]]; then
    echo ${BASH_REMATCH[1]}
    found_number=0
fi

exit $found_number

Tester for Is Quantity Script

It is a script to run a collection of assessments on the isnumber.sh script to confirm that it’s put in and dealing appropriately. Observe that bash is just not at all times situated at /bin/bash. It could even be at /usr/native/bin/bash or different areas in your pc’s file system.

#!/bin/bash
#
# take a look at script for isnumber.sh
#
# Creator: John F. McGowan Ph.D. ([email protected])
# (C) 2015 John F. McGowan
#

# take a look at non-numbers
#
ntests=0
nfails=0

echo "NOT A NUMBER TESTS"
report=`./isnumber.sh canine`
consequence=$?  # must assign this to consequence instantly after isnumber.sh exits
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "PASSED"    
else
    echo "FAILED"
    nfails=`expr $nfails + 1`
fi

report=`./isnumber.sh 123x`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "PASSED"    
else
    echo "FAILED"
    nfails=`expr $nfails + 1`
fi

report=`./isnumber.sh 1.2.3`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "PASSED"    
else
    echo "FAILED"
    nfails=`expr $nfails + 1`
fi

report=`./isnumber.sh 1.2.i`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "PASSED"
else
    echo "FAILED"
    nfails=`expr $nfails + 1`
fi

# take a look at numbers
#
echo "NUMBER TESTS"
# integer
report=`./isnumber.sh 1`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

# zero
report=`./isnumber.sh 0`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

# unfavorable integer
report=`./isnumber.sh -1`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh 1.23`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh .1`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh 0.1`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh af`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh 0xaf`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh "(1,2,3)"`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh [1,2,3]`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh {1,2, 3}`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh 12i`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi


report=`./isnumber.sh 12.i`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

report=`./isnumber.sh .12i`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi


report=`./isnumber.sh 1.2i`
consequence=$?
ntests=`expr $ntests + 1`
if [[ $result == "1" ]]; then
    echo "FAILED"
    nfails=`expr $nfails + 1`
else
    echo "PASSED"
fi

echo " "
echo "SUMMARY"
echo "---------------------------------"
echo "FAILED $nfails OF $ntests TESTS";
if [[ $nfails == 0 ]]; then
    echo "PASSED ALL TESTS!!!!"
fi

# the tip


Instance Profitable Output from Tester

That is an instance of the output when the assessments are all handed.

$ ./test_isnumber.sh 
NOT A NUMBER TESTS
PASSED
PASSED
PASSED
PASSED
NUMBER TESTS
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
PASSED
 
SUMMARY
---------------------------------
FAILED 0 OF 19 TESTS
PASSED ALL TESTS!!!!


© 2015 John F. McGowan

Concerning the Creator

John F. McGowan, Ph.D. solves issues utilizing arithmetic and mathematical software program, together with creating gesture recognition for contact units, video compression and speech recognition applied sciences. He has in depth expertise creating software program in C, C++, MATLAB, Python, Visible Fundamental and lots of different programming languages. He has been a Visiting Scholar at HP Labs creating pc imaginative and prescient algorithms and software program for cellular units. He has labored as a contractor at NASA Ames Analysis Middle concerned within the analysis and improvement of picture and video processing algorithms and expertise. He has printed articles on the origin and evolution of life, the exploration of Mars (anticipating the invention of methane on Mars), and low-cost entry to area. He has a Ph.D. in physics from the College of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Expertise (Caltech). He might be reached at [email protected].