【问题标题】:Ubuntu get the last word in a sentence from a text fileUbuntu从文本文件中获取句子中的最后一个单词
【发布时间】:2012-07-19 23:32:38
【问题描述】:

我有一个制表符分隔的文本文件,结构如下:

word0  word1  word2  word3  word4  word5  word6

从我想要的 linux commond 行:

  1. 只获取word6
  2. 如何只获取 word6 中的字符 ord6?

【问题讨论】:

    标签: linux shell unix ubuntu


    【解决方案1】:


    AWK 可以做到这一点:

     $ echo "word0 word1 word2 word3 word4 word5 word6" | awk '{ print $(NF) }'
     word6
    

    【讨论】:

    • 现在可以完美获取 word6 我将如何更进一步从 word6 获取 ord6 或 wor
    • 将其发送到:sed -E 's/.(...)./\1/',例如,您会得到 ord。
    【解决方案2】:

    您可以使用简单的cut 命令:
    $cut -d'Press CTRL+v then TAB key to insert tab as delim here' -f6 textfile|cut -c2-
    ord6
    $

    查看 cut 命令的手册页了解更多信息。

    当然,还有很多其他方法可以做同样的工作。

    【讨论】:

      【解决方案3】:

      试试这个

      awk -F "\t" '{print $NF}' Input.txt
      

      根据需要更改分隔符。

      【讨论】:

        【解决方案4】:
        string=$(echo "word0 word1 word2 word3 word4 word5 word6" | awk '{ print $(NF) }')
        echo ${string:3:4}  # echo substring starting at postion3, 4 characters long i.e. ord6
        

        echo ${string[@]#w} # removes the shortest possible match for the regex 
                            # (i.e. w) at the start of the string 
        

        ## 最长匹配,% & %% 删除字符串的后面

        【讨论】:

          猜你喜欢
          • 2015-09-08
          • 2014-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多