将字符串分割带符号的bash [英] Split string with bash with symbol

查看:86
本文介绍了将字符串分割带符号的bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个字符串: test1的@测试2

我需要得到这个字符串的测试2 部分。我怎样才能做到这一点使用bash?


解决方案

使用的参数扩展

 海峡='test1的@ TEST2
回声$ {#海峡* @}


  1. 人物说的删除扩展匹配模式的最小preFIX。

  2. 字符的装置的删除扩展匹配模式的最小的后缀。的(所以你可以做 $ {海峡%@ *}来获得测试1部分。)

  3. / 字符的装置的删除扩展符合以下模式的最小和第一子。的Bash有它,但它不是POSIX。

如果您双击它贪婪地匹配模式的字符。


  1. ## 手段的删除扩展匹配模式的最大preFIX。

  2. %% 手段的删除扩展匹配模式的最大后缀。

  3. // 手段的删除扩展匹配模式的所有子。

For example, I have a string: test1@test2.

I need to get the test2 part of this string. How can I do this with bash?

解决方案

Using Parameter Expansion:

str='test1@test2'
echo "${str#*@}"

  1. The # character says Remove the smallest prefix of the expansion matching the pattern.
  2. The % character means Remove the smallest suffix of the expansion matching the pattern. (So you can do "${str%@*}" to get the "test1" part.)
  3. The / character means Remove the smallest and first substring of the expansion matching the following pattern. Bash has it, but it's not POSIX.

If you double the pattern character it matches greedily.

  1. ## means Remove the largest prefix of the expansion matching the pattern.
  2. %% means Remove the largest suffix of the expansion matching the pattern.
  3. // means Remove all substrings of the expansion matching the pattern.

这篇关于将字符串分割带符号的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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