【问题标题】:How to get string between two characters in bash如何在bash中的两个字符之间获取字符串
【发布时间】:2022-11-02 00:09:50
【问题描述】:

如何在两个字符之间获取字符串,例如:

https://hello.world.example.com

//. 之间获得hello 我尝试以下操作:

echo 'https://hello.world.example.com' | awk -F[/.] '{print $2}'

看起来可以解析

如何解析//.之间的字符串?

谢谢!

【问题讨论】:

标签: bash


【解决方案1】:

Bash 本身有字符串操作程序。

尝试:

s='https://hello.world.example.com'

s1="${s#*//}"   # Deletes shortest match of *// from front of $s
s2="${s1%%.*}"  # Delete longest match of .* (ie, from first .) in s1

echo "$s2"
# hello

你可以阅读更多here

【讨论】:

  • 不要只是发布代码,解释它是如何工作的。
猜你喜欢
  • 1970-01-01
  • 2015-11-13
  • 2012-07-07
  • 2020-10-15
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多