重击:在OS X下查找字符串中字符的位置 [英] Bash: Find position of character in a string under OS X

查看:39
本文介绍了重击:在OS X下查找字符串中字符的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在 Mac OS X 下的 Bash 中找到字符串中第一个字符的位置吗?

Is there any way to find a position of a first character within a string in Bash under Mac OS X?

类似的东西:

stringZ=abcABC123ABCabc                      # 6
echo `expr index "$stringZ" C12`             # C position.

高级Bash脚本指南

陷阱:

  1. 官方 index 函数 expr index $ string $ substring 不是存在于OS X(BSD) match
  2. 安装gnu match( gmatch )似乎不是BSD系统领域的便携式解决方案
  1. The official index function expr index $string $substring is not present in OS X (BSD) match
  2. Installing gnu match (gmatch) does not seem to be a portable solution in the realm of BSD systems

有什么想法吗?

推荐答案

这是一个可怕的骇客,可能无法在所有情况下都起作用.

This is a horrible hack, and may not work for all cases.

tmp=${stringZ%%C12*}     # Remove the search string and everything after it
echo $(( ${#tmp} + 1 )) # Add one to the length of the remaining prefix

这篇关于重击:在OS X下查找字符串中字符的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆