【问题标题】:Cut of word from a string and store result to variable从字符串中剪切单词并将结果存储到变量
【发布时间】:2013-11-21 22:33:44
【问题描述】:

如何从字符串中剪切第一个单词,然后将结果存储到变量中?

所以在输入中我有 5 个单词的字符串。

输出将是 4 个单词,没有第一个单词,并且必须存储在变量中。

已经尝试过echo "First Second Third Fourth Fifth" | cut -d " " -f2-,但我无法将结果存储在变量中。

我也不会看到执行结果。

【问题讨论】:

    标签: macos bash shell cut


    【解决方案1】:

    像这样使用command substitution

    result=$(echo "First Second Third Fourth Fifth" | cut -d " " -f2-)
    
    echo "$result"
    

    或者使用纯 BASH:

    s="First Second Third Fourth Fifth"
    echo "${s#* }"
    

    输出:

    Second Third Fourth Fifth
    

    【讨论】:

      【解决方案2】:

      我更喜欢自己,将字符串放在文本文件中,然后你可以这样做:-

      read first second third fourth fifth < file
      

      那么你可以简单地使用:-

      echo "${first}"
      etc
      

      阅读很棒。 :D

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-01
        • 1970-01-01
        • 2017-12-04
        • 1970-01-01
        相关资源
        最近更新 更多