Find first digit in string using expr index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find first digit in string using expr index
# 1  
Old 09-05-2008
Find first digit in string using expr index

I have looked for hours for an answer, so I have decided to request your guidance.

I want to substract the first number (series of digits) contained in a string. This string is the output of another command. The substring (number) can be located at any position inside the string.

I want to use only the bash string manipulation facilities, as to avoid using other commands (sed, awk, etc.).

The Advanced Bash-Scripting Guide: Manipulating Strings section specifies that index is used to get the "Numerical position in $string of first character in $substring that matches".

However, it does not state whether "substring" can be a regular expression or not. After trying things like
Code:
expr index "$string" '[0-9]'
expr index "$string" '[0-9]*'

I reached to the conclusion that substring cannot be a regular expression. Please confirm if this is correct. I know, I learn slow.

Now I found out that this can be solved with a command like
Code:
$(echo "$string" | /bin/awk '{print match($0,"[0-9]")}')

but this is what I wanted to avoid in the first place Smilie

So I want to confirm with you guys if there is a way in which I could accomplish this without using external commands.

Thanks a lot, and I hope this post will eventually help others.

Last edited by jcd; 09-05-2008 at 03:27 PM.. Reason: Adding a link to the String Manipulation section of the Advanced Bash-Scripting Guide
# 2  
Old 09-05-2008
you can use expr match. Check the Manipulating Strings document again. read carefully
# 3  
Old 09-06-2008
What I had in mind yesterday, but didn't have time to finish, was something along the following lines.

Code:
vnix$ s=dsfg123456sdf
vnix$ echo ${s#*[0-9]}
23456sdf
vnix$ echo ${s#${s#*[0-9]}?}
dsfg123456sdf
vnix$ echo ${s%?${s#*[0-9]}}
dsfg
vnix$ echo ${s#${s%?${s#*[0-9]}}}
123456sdf
vnix$ t=${s#${s%?${s#*[0-9]}}}
vnix$ echo ${t%[0-9]*}
12345
vnix$ echo ${t#${t%[0-9]*}?}
sdf
vnix$ echo ${t%${t#${t%[0-9]*}?}}
123456

It's probably too cumbersome to be of much practical utility, but it does work without any external command (as long as there is a single numeric substring).

Here's another rather monstrous idea (you can do it without Perl, although I would hate to have to):

Code:
vnix$ s=sdfd345sdfg436efg
vnix$ OLDIFS=$IFS
vnix$ IFS=`perl -e 'print map { chr($_) } (0..0x2F, 0x3A..0x7E)'`
vnix$ set -- $s
vnix$ IFS=$OLDIFS
vnix$ echo "$@"
    345    436

# 4  
Old 09-06-2008
Hammer & Screwdriver One way, although using tr & head & tail

Code:
> echo $s
dsfg123456sdf
> echo $s | tr [a-z] " " | tr -s " " | tr " " "\n" | head -2 | tail -1
123456

# 5  
Old 09-07-2008
Let's go shorter
Code:
echo $s | tr -d a-z
.. or 
tr -d a-z <<< $s

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Getting first digit of a string

If i'm given a string like "abc-def-1.2.3", how would I return "1"? I'm new to scripting and got stumped on this problem. Thanks in advance! (7 Replies)
Discussion started by: atsim
7 Replies

2. Shell Programming and Scripting

How to use expr INDEX with pipe?

Hi, I need to use the output of previous command in the next command while using pipe. Like I am reading a file then I am fetching first line from a file and then I want to know the location of character 'e' in that line. cat filename|sed -n 1p|expr index ------ e In above example I... (5 Replies)
Discussion started by: Peeyush Sehgal
5 Replies

3. Shell Programming and Scripting

How to delete only the last digit in string

Hello, I would like to convert this string KBL3TEST1 into KBL3TEST How can i code this? Any help is appreciated regards, blashyou (6 Replies)
Discussion started by: blashyou
6 Replies

4. Shell Programming and Scripting

Check whether a string begin with uppercase, lowercase or digit!

Hi every body! I wrote script on Fedora (bash shell) to check whether a tring enter from user console is start with a uppercase/lowercase letter or a digit. But with this script i have some problem when I enter from character from 'b' to 'z' --> result is uppercase. This code look like ok but i... (9 Replies)
Discussion started by: nguyendu0102
9 Replies

5. UNIX for Dummies Questions & Answers

how to get index/postion of a string?

Hi, I have a string like the following: /db1/data/GLIDER/SYSTEM.dbf need to find the postion where "SYSTEM.dbf" starts, so I tried: LOCATION=/db1/data/GLIDER/SYSTEM.dbf $ expr index $LOCATION SYSTEM expr: syntax error $ expr index "$LOCATION" SYSTEM expr: syntax error ... (5 Replies)
Discussion started by: seafan
5 Replies

6. Shell Programming and Scripting

Creating 12 digit string value

Hi Masters, here is my req I have to create a 12 digit string which includes the user i/p Like if user input 2334 then the string will be 233411111111 ,if the user inputs 23345 then the string will be 233451111111 , So we dont know how many digits will the user inputs output will be 12... (16 Replies)
Discussion started by: Pratik4891
16 Replies

7. Programming

Find out 2^n+1 , where n is a 3 digit number

I have to write a c program which takes a 3 digit number n and calculates the value of (2^n)+1 and then determines the number is prime or not. I have tried to first calculate the value of 2^n and then adding one to it and then apply the logic of prime number. but the ultimate problem is that... (7 Replies)
Discussion started by: agrawal.prachi
7 Replies

8. UNIX for Dummies Questions & Answers

string index

I have a line "My name is Deepak" How can i search a string Deepak in the line and find out its index position. Here in this case the result should be 12. (3 Replies)
Discussion started by: dr46014
3 Replies

9. Shell Programming and Scripting

Find index of last occurence of a character within a string

I need to find the index of last '|' (highlighted in bold) in awk : |ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if Please suggest a way... Thanks (5 Replies)
Discussion started by: joyan321
5 Replies

10. Shell Programming and Scripting

why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit?

I found below script to check whether the variable is a digit in ksh. ############################ #!/bin/ksh REPLY="3f" if ]*\)'` != ${REPLY} && "${REPLY}" != "0" ]] then print "is digit\n" else print "not digit\n" fi ############################ Although it works fine, but... (6 Replies)
Discussion started by: sleepy_11
6 Replies
Login or Register to Ask a Question