next up previous 405
Next: Extracting a substring
Up: String Processing
Previous: Obtain the length of a string


Find the position of a substring

This requires the awk function index. This returns zero if the string could not be located. Note that comparisons are case sensitive.

     set catal = "NGC "
     set number = 2345
     set object = "Processing $catal$number."
     set cind = `echo $object | awk '{print index($0,"ngc")}'`      # =  0
     set cind = `echo $object | awk '{print index($0,"NGC")}'`      # = 12

     set places = ( Jupiter "Eagle Nebula" "Gamma quadrant" )
     set cposa = `echo $places | awk '{print index($0,"ebu")}'`     # = 16
     set cposn = `echo $places | awk '{print index($0,"alpha")}'`   # =  0
     set cpos1 = `echo $places[1] | awk '{print index($0,"Owl")}'`  # =  0
     set cpos2 = `echo $places[2] | awk '{print index($0,"ebu")}'`  # =  8
     set cpos3 = `echo $places[3] | awk '{print index($0,"rant")}'` # = 11
An array of strings is treated as a space-separated list of the elements. The double quotes are delimiters; they are not part of the string so are not counted.



next up previous 405
Next: Extracting a substring
Up: String Processing
Previous: Obtain the length of a string

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:starlink@jiscmail.ac.uk

Copyright © 2013 Science and Technology Facilities Council