Sponsored Content
Top Forums Shell Programming and Scripting Find first digit in string using expr index Post 302232929 by jcd on Friday 5th of September 2008 02:23:49 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
CTYPE_ALNUM(3)								 1							    CTYPE_ALNUM(3)

ctype_alnum - Check for alphanumeric character(s)

SYNOPSIS
bool ctype_alnum (string $text) DESCRIPTION
Checks if all of the characters in the provided string, $text, are alphanumeric. PARAMETERS
o $text - The tested string. RETURN VALUES
Returns TRUE if every character in $text is either a letter or a digit, FALSE otherwise. EXAMPLES
Example #1 A ctype_alnum(3) example (using the default locale) <?php $strings = array('AbCd1zyZ9', 'foo!#$bar'); foreach ($strings as $testcase) { if (ctype_alnum($testcase)) { echo "The string $testcase consists of all letters or digits. "; } else { echo "The string $testcase does not consist of all letters or digits. "; } } ?> The above example will output: The string AbCd1zyZ9 consists of all letters or digits. The string foo!#$bar does not consist of all letters or digits. NOTES
Note If an integer between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative val- ues have 256 added in order to allow characters in the Extended ASCII range). Any other integer is interpreted as a string contain- ing the decimal digits of the integer. SEE ALSO
ctype_alpha(3), ctype_digit(3), setlocale(3). PHP Documentation Group CTYPE_ALNUM(3)
All times are GMT -4. The time now is 06:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy