string - Bash:从字符第一次出现到第二次出现的子字符串

标签 string bash substring character find-occurrences

在 bash 中,如何获取从第一次出现的字符到第二次出现的同一字符的所有内容的子字符串。

例子...

Input String = "abc-def-ghi"

Character = "-"

Desired Output String = "def"

最佳答案

我会使用两个参数扩展。

str="abc-def-ghi"
tmp=${str#*-}  # Remove everything up to and including first -
result=${tmp%%-*} # Remove the first - and everything following it

关于string - Bash:从字符第一次出现到第二次出现的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45921699/

相关文章:

python - Z3Python : StringSort support

bash - 如何在shell脚本中处理Ctrl + c?

linux - 最大文件的打印尺寸和平均文件尺寸

javascript - 使用 Javascript 根据多个字符从格式错误的 URL 中提取字符串

c# - 使字符串 URL 友好(例如 : convert Montréal to Montreal)

string - C++/CLI 将 System::String 转换为 const char*

java - 如何处理Java中字符串的特定位置?

postgresql - PG::Error: ERROR: function left(character varying, integer) 不存在

java - 使用多线程读取多个txt文件搜索字符串

bash - Shell 条件错误 - Bash 语法无法识别