Determine if first 2 digits of string match numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Determine if first 2 digits of string match numbers
# 1  
Old 08-13-2015
Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22"
Not sure how.
I could use

Code:
 if [[ "$file" =~ 22 ]]; then echo "yes";fi

But I think that will only grab the pattern 22 and not the first 2 digits.
# 2  
Old 08-13-2015
Try:
Code:
 if [[ "${file:0:2}" = 22 ]]; then echo "yes";fi

This will start 'reading' the string from 0 (the start) until character 2, so the first two.

hth
# 3  
Old 08-13-2015
You could use this for any leading pattern:
Code:
PAT=22
[ ${file#$PAT} == ${file} ] && echo no || echo yes
yes

# 4  
Old 08-13-2015
Quote:
Originally Posted by newbie2010
Trying to find out how to discover if the first 2 characters of a string are "22"
Not sure how.
I could use

Code:
 if [[ "$file" =~ 22 ]]; then echo "yes";fi

But I think that will only grab the pattern 22 and not the first 2 digits.
You are correct, it will match the pattern any where in the string variable. However, adding the beginning of line anchor will produce what you want.
Code:
 if [[ $file =~ ^22 ]]; then echo yes;fi


Last edited by Aia; 08-13-2015 at 08:02 PM..
This User Gave Thanks to Aia For This Post:
# 5  
Old 08-14-2015
You could also do it with perhaps a slightly cheeky variable substitution in ksh:-
Code:
if [ "${file#22}" != "$file" ] && echo Yes || echo No

The substitution cuts off the leading 22 if it exists, therefore you get differences for a match and no differences if it does not start with 22


Just an alternate Smilie

Robin
# 6  
Old 08-14-2015
Or use a glob-match:
Code:
if [[ "$file" == 22* ]]; then echo "yes";fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

2. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

3. Shell Programming and Scripting

Regex - Return numbers of exactly 8 digits

Hi All I am new to this forum and also regex. I am using bash scripting and have a file like this "0012","efgh","12345678","adfdf", "36598745" "87654321","hijk","lmno" I want the ouput to be 12345678 36598745 87654321 Criteria like this - number - 8 carachters long Please let... (21 Replies)
Discussion started by: buttseire
21 Replies

4. Shell Programming and Scripting

Sed only digits not in numbers

Hi, I have a text file with an array of numbers such as : 123 1 456 45 9817 1 45 I would like to replace the digit "1" in a text file with "A". So it looks like this: 123 A 456 45 9817 A 45 If I use sed 's/1/A/g', I get A23 A 456 45 98A7 A 45 I... (3 Replies)
Discussion started by: jejeking
3 Replies

5. Shell Programming and Scripting

Use match() in nawk to find digits in number

Hi, I just need to check whether number of digits in a phone number is 10 or not. If I am not wrong regex will be: {9} I have to use this inside nawk as this is a small portion of a big program. nawk ' BEGIN { RS="";FS=";"; regex="{9}"; } { for (i=1;i<=NF;i++) { if... (6 Replies)
Discussion started by: shekhar2010us
6 Replies

6. Shell Programming and Scripting

regex to match digits not in dates

hi all, im having problems. I need to change all number 10 in a text file to word form, or in short from 10->ten. the thing is number 10 including in dates such as 10/22/1997 or 03-10-2011 should not be changed. im having some trouble because the file contains numbers like "price range from... (11 Replies)
Discussion started by: perlishell
11 Replies

7. UNIX for Dummies Questions & Answers

problem to determine all files and dir match up on 2 different unix boxes

Hi Friends I have 2 solaris boxes and I need to check certain directories (on local filesystem and mounted nfs) to make sure that they match up on both boxes and to delete or move the other mismatches to elsewhere on the local filesystem. I investigated for unix commands like rsync, and tree... (1 Reply)
Discussion started by: mpc8250
1 Replies

8. Shell Programming and Scripting

find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format. ua07app201 (20 Replies)
Discussion started by: honglus
20 Replies

9. UNIX for Dummies Questions & Answers

to check if a string has only digits

Hi guys, I am not very experienced in writing ksh scripts and I am trying to write a piece of code that indicates if a given string contains only digits and no alphabet (upper or lower case). If i write it my way it would turn out to have a lot of comparisons.. :eek: Thanks a lot in... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

How to pattern match on digits and then increment?

I have a log file that ends in a ".xxx" where xxx are digits but I don't necessarily know what digits they are. The log file rotates automatically and is auto-incrementing - starting at .001. So the example would be: file-name.005 If the file ends in .005 and the log rotates, it logically... (2 Replies)
Discussion started by: sdutto01
2 Replies
Login or Register to Ask a Question