how to get index/postion of a string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to get index/postion of a string?
# 1  
Old 03-11-2011
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

What am I doing wrong there?

Thanks in Advance.

Last edited by seafan; 03-11-2011 at 05:27 PM..
# 2  
Old 03-11-2011
What is your system? What is your shell?
# 3  
Old 03-11-2011
Quote:
Originally Posted by Corona688
What is your system? What is your shell?

$ uname -a
SunOS demccuatux02 5.10 Generic_127127-11 sun4us sparc FJSV,GPUZC-M

$ ps -ef | grep $$
oracle 28321 28393 0 16:23:20 pts/1 0:00 -ksh
oracle 6138 28321 0 16:58:25 pts/1 0:00 grep 28321

I just found out that "index" is not in the path -


$ which index
no index in /local/app/oracle/product/10.2.0/db_1/bin /local/app/oracle/product/10.2.0/db_1/OPatch /usr/bin .


now the question becomes, are there certain version of unix that doesn't include commands like index? how to figure out the path to it if they are there?
# 4  
Old 03-11-2011
index isn't a literal command, it's part of the expr utility or builtin. Since many shells have it as a shell builtin, what features you get may depend on what shell you use.

You could try forcing it to use an external expr by giving expr's absolute path, i.e. /bin/expr
# 5  
Old 03-11-2011
Question

Quote:
Originally Posted by Corona688
index isn't a literal command, it's part of the expr utility or builtin. Since many shells have it as a shell builtin, what features you get may depend on what shell you use.

You could try forcing it to use an external expr by giving expr's absolute path, i.e. /bin/expr

thanks for clarifying that. so the question is back to, why am I getting a syntax error?
# 6  
Old 03-11-2011
As you are using Solaris it is likely that index is not part of the expr functionality.In fact the expr man page on Solaris says that it is for interactive use only. You will have to use another method such as deleting that portion from the string and counting the length, or using a perl one-liner.

Andrew
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing the value of a string index to a variable.

Hi, coding a simple program to compare an entered number to a randomly generated one. The number of digits are restricted so I'm just trying to figure out how to refer to the index value in a string and then compare it to the variable I want. I don't know if bash automatically indexes strings, so... (7 Replies)
Discussion started by: outofcookies
7 Replies

2. Shell Programming and Scripting

Substitute string with an index number

Objective is to substitute Jan with 01, Feb with 02 and so on. The month will be provided as input. I could construct below awk and it worked. echo Jun | \ awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",mon," ")}{ for (i=1;i<=12;i++){ if ($1==mon) printf("%02d\n",i)} }' ... (4 Replies)
Discussion started by: krishmaths
4 Replies

3. Shell Programming and Scripting

To cut a string based on last index of identifier

here below is sample string null pointer dereference of 'resourceList' where null is returned from a method/opt/bld/fetch/ds/interzone/notification/LocalLineStatusNotificationListener.java:79 null pointer dereference of 'reList' where null is returned from a... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

awk uniq and longest string of a column as index

I met a challenge to filter ~70 millions of sequence rows and I want using awk with conditions: 1) longest string of each pattern in column 2, ignore any sub-string, as the index; 2) all the unique patterns after 1); 3) print the whole row; input: 1 ABCDEFGHI longest_sequence1 2 ABCDEFGH... (12 Replies)
Discussion started by: yifangt
12 Replies

5. Shell Programming and Scripting

check index of a string by finding a letter in it

i would like to search for a letter in a string and get its index position. example: name='john' pos=$(expr index $name o) the result will be equal to 2 (2nd position) how do you make this thing not case sensitive? example: name='john' pos=$(expr index $name O) the... (1 Reply)
Discussion started by: kokoro
1 Replies

6. Shell Programming and Scripting

Finiding filenames with specific index string

Hi All, I have a file (Names.txt) and the contents of the file is give below. $ cat Names.txt FF313207008.txt FF223207007.txt FF143207006.txt FF372150600.txt FF063407005.txt FF063307005.txt $ From these given file names I want to find the files which has the 6th index value as 2. So... (5 Replies)
Discussion started by: krish_indus
5 Replies

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

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

9. Shell Programming and Scripting

Problem when assign the array with the string index

I come across the problems when assigning the array in the script below . How to use the array with the 'string index' correctly ? When I assign a new string index , the array elements that are previously assigned are all changed .:eek::eek::eek: $ array=211 $ echo ${array} 211 $... (4 Replies)
Discussion started by: youareapkman
4 Replies

10. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: jcd
4 Replies
Login or Register to Ask a Question