【问题标题】:Delete the line prior to the line containing the search pattern using linux command使用 linux 命令删除包含搜索模式的行之前的行
【发布时间】:2019-06-01 22:35:17
【问题描述】:

我有一个包含单词和一些字符的 yaml 文件。 version: "5.0.0" migratorConfigs: - name: "SchemaMigrator" order: 1 parameters: location: "step1" schema: "identity" - version: "5.1.0" migratorConfigs: - name: "IdentityDataCleaner" order: 1 parameters: schema: "identity" - name: "SchemaMigrator" order: 2 parameters: location: "step1" schema: "identity" - name: "RegistryDataMigrator" order: 6 parameters: schema: "um"

我只需要删除name: "RegistryDataMigrator" 之前的破折号(-)。有很多破折号,我只想删除最后一个。

有什么想法吗?

【问题讨论】:

    标签: shell sed text-parsing


    【解决方案1】:

    我刚刚从

    找到了这个答案

    http://www.theunixschool.com/2012/06/sed-25-examples-to-delete-line-or.html

    sed -i -n '/RegistryDataMigrator/{x;d;};1h;1!{x;p;};${x;p;}' /home/user1/Documents/migration.yaml
    

    它对我有用!

    【讨论】:

      【解决方案2】:

      这可能对你有用(GNU sed):

      sed 'N;/RegistryDataMigrator[^\n]*$/!P;D' file
      

      使用N;...;P;D 方法在文件的整个长度上创建一个两行窗口,如果窗口的最后一行包含RegistryDataMigrator,则不打印窗口的第一行。

      【讨论】:

        猜你喜欢
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-14
        • 1970-01-01
        • 2018-03-12
        相关资源
        最近更新 更多