Check if a string starts with certain values and ends with numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a string starts with certain values and ends with numbers
# 1  
Old 09-02-2016
Check if a string starts with certain values and ends with numbers

This is very basic. Yet Iam struggling to get the right pattern for my check.
Apologize in advance to ask a very lame question.

I have to validate if a value of the variable starts with "efgh" and followed by 6 numbers.

Var1="efgh234567"

The condition Iam trying to achieve is similar to the below.
Code:
if [[ ${Var1} == "^efgh[0-9][0-9][0-9][0-9][0-9][0-9]$" ]]; then
....

# 2  
Old 09-02-2016
Quote:
Originally Posted by deepakwins
The condition Iam trying to achieve is similar to the below.
Code:
if [[ ${Var1} == "^efgh[0-9][0-9][0-9][0-9][0-9][0-9]$" ]]; then
....

Your code won't work. You are providing a regular expression, but the == operator expects a glob pattern. You could either use the regexp operator (=~) or write your pattern as a glob-pattern:

Code:
[[ $Var1 == efgh[0-9][0-9][0-9][0-9][0-9][0-9] ]];

# 3  
Old 09-02-2016
Thanks for pointing out the obvious Rovf!
Some how, when I tried the above, it taking anything after "efgh". Its working as if its like "efgh*".
Eg: "efghTest" is also working ok with the check.

Perhaps, could you help me with the syntax for reqexp operator?
# 4  
Old 09-02-2016
Sure?

Code:
Var1=efgh123456
[[ $Var1 == efgh[0-9][0-9][0-9][0-9][0-9][0-9] ]] && echo OK || echo NOK
OK
Var1=efghTest
[[ $Var1 == efgh[0-9][0-9][0-9][0-9][0-9][0-9] ]] && echo OK || echo NOK
NOK

# 5  
Old 09-02-2016
Sorry sir, my bad, I had a typo in my statement. It perfectly worked.
Thanks for your time RudiC!

Iam still curious for the RegEx option though. Smilie
# 6  
Old 09-02-2016
Code:
[[ ${Var1} =~ ^efgh[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && echo OK || echo NOK
OK

or

Code:
[[ ${Var1} =~ ^efgh[0-9]{6}$ ]] && echo OK || echo NOK

# 7  
Old 09-02-2016
Quote:
Originally Posted by deepakwins
Perhaps, could you help me with the syntax for reqexp operator?
From the bash man page:

An additional binary operator, =~, is available, with the same precedence as == and
!=. When it is used, the string to the right of the operator is considered an
extended regular expression and matched accordingly (as in regex(3)). The return
value is 0 if the string matches the pattern, and 1 otherwise. If the regular
expression is syntactically incorrect, the conditional expression's return value is
2. If the shell option nocasematch is enabled, the match is performed without regard
to the case of alphabetic characters. Any part of the pattern may be quoted to force
the quoted portion to be matched as a string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string contains substring surrounded by numbers

Hi, I have a process that generates strings. I would like to check each string and search for substring which contains the letter 'E' surrounded by numbers (both sides of the letter 'E'). few examples: AA4E7012A2 - contains E surrounded by numbers FE18274012 - does not contain E... (3 Replies)
Discussion started by: yanive
3 Replies

2. Shell Programming and Scripting

Need to write a shell script that starts one, then kills it, then starts another?

This is on a CentOS box, I have two scripts that need to run in order. I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which... (3 Replies)
Discussion started by: btramer
3 Replies

3. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Swapping a string of numbers between higher and lower order values(HEX)

I have this below string in a variable cutString=21222222222222222122222222222222 this string is nothing but hex values depicted as below 21:22:22:22:22:22:22:22:21:22:22:22:22:22:22:22 so what i want to achieve is swap the lower order with higher order values in the... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

6. Shell Programming and Scripting

How to check if a filename in a directory starts with a certain string

Hello, Trying to iterate over set of file in current directory and check if the file name in that folder matches certain string. This is what I have so far. Here I am checking if the file name starts with nexus, if so echo file name to log file. Getting weird syntax errors. Any help is... (7 Replies)
Discussion started by: scorpioraghu
7 Replies

7. Shell Programming and Scripting

Check if string starts with $ symbol

How do I check that a string $AA22CC3 starts with the "$" symbol ? I have tried : checksum='$AAB3E45' echo $checksum case $checksum in $* ) echo success ; esac Thanks ... (4 Replies)
Discussion started by: cillmor
4 Replies

8. Shell Programming and Scripting

Check if a variable ends in a particular number

Hi guys, I am working on a server where there are many users. The user names end in a 1 or a 2. I want to write a bash script that will say which users are in which group and was wondering if I could get some help. The only part I am unsure of is how to check if it ends in the number. Here's... (2 Replies)
Discussion started by: wua05
2 Replies

9. Shell Programming and Scripting

Changing values with increasing numbers!

Hi all, i have a command named "vmchange" and i must use it for thousands of data which must be changed. For example, vmchange -m N0001 vmchange -m N0002 vmchange -m N0003 ... ... vmchange -m N0100 How can i do that in awk or bash script? Any help would be greatly appreciated.. ... (5 Replies)
Discussion started by: oduth
5 Replies

10. Shell Programming and Scripting

i want to know entered string ends with .jar

Hi all, I want to know whelther string entered ends with .jar. Can any one help please.. Thank U Naree (3 Replies)
Discussion started by: naree
3 Replies
Login or Register to Ask a Question