【问题标题】:Find all instances of word occurring in a file followed by an equal sign and quotation mark查找文件中出现的所有单词实例,后跟等号和引号
【发布时间】:2018-03-13 02:08:16
【问题描述】:

我有一个文件中包含单词 system="Some Value" 并且该文件是 1 长行。

如果文件格式正确,我知道我可以执行以下操作:

cat filename | grep 'system="'

我会一行一行地收到它们。但是由于文件有很多段落,并且每个段落有 1 行长,我不能。

最好将系统这个词加上它的值一直转储到另一个文件中,如下所示:

system="Some words"
system="Some other words"
system="and more"

【问题讨论】:

  • 请提供一个示例文件及其内容,以及所需的输出
  • 没有输入文件很难判断,但grep -o 'system="[^"]*"' filename 可以工作。

标签: string bash find


【解决方案1】:

本杰明答对了。

grep -o 'system="[^"]*"' filename > final.txt

看起来效果不错。

感谢您的帮助!

【讨论】:

    【解决方案2】:

    由于您还没有向我们展示您的 Input_file,所以我正在考虑以下是您的 Input_file。

    cat Input_file
    system="Some words" system="Some other words" system="and more"
    

    下面是帮助我们将输出作为您显示的输出的代码。

    awk '{gsub(/ system/,"\nsystem");print}'  Input_file
    

    输出如下。

    system="Some words"
    system="Some other words"
    system="and more"
    

    解释:只需使用 awk 的全局替换实用程序将所有出现的字符串 system 替换为 newline system。然后打印该行。

    编辑:或者使用 sed 解决方案,您可以简单地将所有空间系统替换为新行系统,如下所示。

    sed 's/ system/\nsystem/g'  Input_file
    

    【讨论】:

      猜你喜欢
      • 2013-12-12
      • 1970-01-01
      • 2017-06-16
      • 2015-09-16
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多