Visit Our UNIX and Linux User Community


Testing Var for 3 digits in a row only


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Testing Var for 3 digits in a row only
# 1  
Old 07-31-2009
Testing Var for 3 digits in a row only

Hi,

I want to test a var, $inputPin to see if is 3 digits in a row or not in a Bash script.
I am getting stuck on the regex to do this.
And the equality test if it is in an if statement as below?
-ne is for comparing numbers
But I guess it would be != in this case
Code:
if [$inputPin !=  [[0-9]\{3,3\]}]

Simple for someone I'm sure. But I am stumped.

cheers

N
# 2  
Old 07-31-2009
You can't put a regular expression into a string comparison like that, you have to use a regular expression operator:

Code:
if [[ "$STR" =~ ^[0-9][0-9][0-9]$ ]]
then
    echo "Three digits"
fi

Note that this will ONLY work in bash, not ksh or anything, since only bash has regex support like this. You could also do it with case, which should work in more shells:

Code:
 case "$STR" in
[0-9][0-9][0-9])
        echo "Three digits"
        ;;
*)
        ;;
esac

# 3  
Old 07-31-2009
Or:

Code:
if [[ ! $inputPin =~ "[0-9]{3,3}" ]]

Regards
# 4  
Old 07-31-2009
Thanks to all for posting.

Have ended up using this code, corona688's for time being.
Code:
if [[ "$STR" =~ ^[0-9][0-9][0-9]$ ]]
then
    echo "Three digits"
fi

Code:
if [[ ! $inputPin =~ "[0-9]{3,3}" ]]

Didn't seem to work for me. Returned ?

But thanks anyway.

cheers

Nick
# 5  
Old 07-31-2009
Quote:
Originally Posted by de_la_espada
Code:
if [[ ! $inputPin =~ "[0-9]{3,3}" ]]

Didn't seem to work for me. Returned ?
You can't put quotes around a BASH regex. And without the ^ and $ it would match 000a, a000, a000a, and so forth.
# 6  
Old 07-31-2009
Quote:
Note that this will ONLY work in bash, not ksh or anything, since only bash has regex support like this.
Works perfectly well with ksh93 although the more common way is as follows
Code:
if [[ $STR == {3}([[:digit:]]) ]]
then
    print "Three digits"
fi

# 7  
Old 08-01-2009
Quote:
Originally Posted by Corona688
You can't put quotes around a BASH regex. And without the ^ and $ it would match 000a, a000, a000a, and so forth.
With the quotes it works fine for me with bash!

Any way, to match exact 3 digits (without quotesSmilie):

Code:
if [[ $zz =~ ^[0-9]{3,3}$ ]]
then
    echo "Three digits"
fi

Regards
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #152
Difficulty: Easy
In 1995, Novell transferred the Unix trademarks to the ACME Computing Company.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

5. Shell Programming and Scripting

Remove duplicates and update last 2 digits of the original row with 0's

Hi, I have a requirement where I have to remove duplicates from a file based on the first 8 chars (It is fixed width file of 10 chars length) and whenever a duplicate row is found, its original row's last 2 chars should be updated to all 0's. I thought of using sort -u -k 1.1,1.8... (4 Replies)
Discussion started by: farawaydsky
4 Replies

6. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

7. UNIX for Advanced & Expert Users

Testing privileges -lock lockfile /var/lock/subsys/..- Permission denied

Hi all, I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...). I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message. Is there any way to simply get a return code... (3 Replies)
Discussion started by: Dedalus
3 Replies

8. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

9. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

10. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

Featured Tech Videos