title: Counting char in word with different delimiter
body: <p>I am writing a shell script, in which I get the location of java via which java. As response I get (for example) </p>
  
  <p><code>/usr/pi/java7_32/jre/bin/java</code>.</p>
  
  <p>I need the path to be cut so it ends with /jre/, more specificly</p>
  
  <pre><code>/usr/pi/java7_32/jre/
  </code></pre>
  
  <p>as the programm this information is provided to can not handle the longe path to work.</p>
  
  <p>I have used cut with the / as delimiter and as I thought that the directory of the Java installation is always the same, therfore a </p>
  
  <pre><code>cut -d'/' -f1-5
  </code></pre>
  
  <p>worked just fine to get this result: </p>
  
  <pre><code>/usr/pi/java7_32/jre/
  </code></pre>
  
  <p>But as the java could be installed somewhere else aswell, for example at</p>
  
  <pre><code>/usr/java8_64/jre/
  </code></pre>
  
  <p>the statement would not work correctly.</p>
  
  <p>I need tried sed, awk, cut and different combinations of them but found no answer I liked.</p>
  
  <p>As the title says I would count the number of appereance of the car <code>/</code> until the substing <code>jre/</code> is found under the premisse that the shell counts from the left to the right. </p>
  
  <p>The incremented number would be the the field I want to see by cutting with the delimiter.</p>
  
  <pre><code>path=$(which java) # example: /usr/pi/java7_32/jre/bin/java
  i=0
  
  #while loop with a statment which would go through path
  
  while substring != jre/ {
  
    if (char = '/') 
      i++
  }
  
  #cut the path 
  path=$path | cut -d'/' -f 1-i  
  
  #/usr/pi/java7_32/jre result 
  </code></pre>
  
  <p>Problem is the eventual difference in the path before and after </p>
  
  <pre><code>/java7_64/jre/, like   */java*/jre/
  </code></pre>
  
  <p>I am open for any ideas and solutions, thanks a lot! </p>
  
  <p>Greets
  Jan</p>