Find index of last occurence of a character within a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find index of last occurence of a character within a string
# 1  
Old 06-19-2009
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

Last edited by joyan321; 06-19-2009 at 12:06 PM..
# 2  
Old 06-19-2009
how about awk:
Code:
echo "|ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if"   |
  awk -F'|' ' x= length($0) - length($NR);  print x - 1'

# 3  
Old 06-19-2009
try this

Code:
awk -v RS="|"  '(NR<4) {x+=length($0) } END{print x+3}' inputfile.txt

BR
# 4  
Old 06-19-2009
Code:
echo '1234|56|7|890' | nawk '
function rindex(str,c)
{
  return match(str,"\\" c "[^" c "]*$")? RSTART : 0
}
{
  print rindex($0, "|")
}'

# 5  
Old 06-19-2009
Code:
awk 'BEGIN{FS=""}{ for(i=1;i<=NF;i++){ if($i=="|"){ p=i } }}END{  print p }' file

# 6  
Old 06-20-2009
Quote:
Originally Posted by joyan321
I need to find the index of last '|' (highlighted in bold) in awk :

|ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if

Code:
_rindex() 
{ 
    case $1 in 
        *$2*)
            idx=${1%$2*};
            _RINDEX=$(( ${#idx} + 1 ))
        ;;
        *)
            _RINDEX=0;
            return 1
        ;;
    esac
}

Code:
$ _rindex '|ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if'
$ echo $_RINDEX
54

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

2. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

3. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

4. Shell Programming and Scripting

Find the occurence of particular string in log file

I have a log file which looks like this: <845185415165:STATUS:5/0:0:0:0:0|ghy59DI5zasldf87asdfamas8df9asd903tGUVSQx4GJVSQ==> I have to extract DATE and number of times the keyword STATUS is shown on each date. Input is : <1354625655744:STATUS:5/0:0:0:0:0|ghy59DI5ztGUVSQx4GJVSQ==>... (8 Replies)
Discussion started by: maddyrox
8 Replies

5. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

shell script to find the second occurence of the alphabet in a string

this is my assignment question. i'm supposed to submit it tommorow. can somebody please help me with it? Do not post homework questions in the main forums. Please post in the homework forum using the correct template. (0 Replies)
Discussion started by: vijjy
0 Replies

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

8. Shell Programming and Scripting

To find a character immediately following a specified String

Hello, I have a UNIX file in which data is like this -- ISA*00* *00* *01*006415160 *01*137361242 ... (3 Replies)
Discussion started by: The Observer
3 Replies

9. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

10. Shell Programming and Scripting

How to find vowel's occurence in a string

Hi All, I want to search the string for vowel's occurence and find the no of occurence of each vowels, Could anyone help me out? This is urgent to me...I m new to Shell programming.. Thanks and Regards, Nids:b: (4 Replies)
Discussion started by: Nidhi2177
4 Replies
Login or Register to Ask a Question