【问题标题】:trim last word from string from commandline in linux从 linux 的命令行中修剪字符串中的最后一个单词
【发布时间】:2021-02-16 13:53:13
【问题描述】:

我试图从字符串中删除最后一个单词,但无法获取该单词。

请看下面我尝试了两种方法但没有运气

$ test="COUNT(*) ---------- 10"   <-string
$ echo $test
COUNT(*) ---------- 10
$ echo ${test##*:}    <-First way
COUNT(*) ---------- 10   <-output
$ echo $test | rev | cut -d: -f1 | rev   <-second way
COUNT(*) ---------- 10 <-output

我需要 o/p 为 10

【问题讨论】:

    标签: linux bash shell terminal


    【解决方案1】:
    last_word=${test##* }
    

    ## 选择要删除的最大字符串,要删除的字符串必须以空格结尾。

    【讨论】:

      【解决方案2】:
      echo ${test##* }
      

      来自 man bash:

         ${parameter##word}
                Remove matching prefix pattern.  The word is expanded to produce
                a pattern just as in pathname expansion.  If the pattern matches
                the beginning of the value of parameter, then the result of  the
                expansion  is  the expanded value of parameter with the shortest
                matching pattern (the ``#'' case) or the longest  matching  patâ
                tern  (the  ``##''  case)  deleted.  If parameter is @ or *, the
                pattern removal operation is applied to each positional  parameâ
                ter in turn, and the expansion is the resultant list.  If paramâ
                eter is an array variable subscripted with @ or *,  the  pattern
                removal  operation  is  applied  to  each member of the array in
                turn, and the expansion is the resultant list.
      

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        • 2011-02-13
        • 1970-01-01
        • 2020-02-14
        • 2016-01-21
        相关资源
        最近更新 更多