【问题标题】:Git Bash - find words in a file (or string) matching specified sub-stringGit Bash - 在文件(或字符串)中查找与指定子字符串匹配的单词
【发布时间】:2014-11-03 19:21:38
【问题描述】:

cmd: cat test.txt | grep 引脚
结果:打印所有包含 pin 的行

我现在只想对包含 pin 的单词进行 grep。 这样做的命令是什么?

谢谢!

大家好,谢谢你们的cmets。 我正在使用 Git Bash(版本 1.9.4)。 此 shell 中的 grep 没有 -o 选项。 有一个 -w 选项。 我试过: grep -w 'pin' test.txt 但它什么也没返回。

有人使用 Git Bash 来解决这个问题吗?

谢谢大家。

【问题讨论】:

  • 您可以通过grep -o 只打印一行的匹配部分。
  • grep -o '\w*pin\w*' test.txt

标签: bash word


【解决方案1】:

你可以使用:

grep -o '[^[:blank:]]*pin[^[:blank:]]*' test.txt

【讨论】:

    【解决方案2】:

    假设您的文件名为test.txt,您可以这样做:

    grep -o '\S*pin\S*' test.txt

    -o 标志将只打印该行上的匹配词,而不是整行。

    【讨论】:

      【解决方案3】:

      您可以使用 -w 选项。

      $ cat test
      pin
      PIN
      somepin
      aping
      spinx
      $ grep pin test
      pin
      somepin
      aping
      spinx
      $ grep -w pin test
      pin
      $
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-17
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        相关资源
        最近更新 更多