Search code examples
shellsedtext-parsing

Delete the line prior to the line containing the search pattern using linux command


I have a yaml file with words and some characters. 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"

I need to remove only the dash(-) prior to name: "RegistryDataMigrator". There were many dashes and I just wanted to remove the last one.

Any ideas please?


Solution

  • This might work for you (GNU sed):

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

    Create a two line window throughout the length of the file using the N;...;P;D method and do not print the first line of the window if the last line of the window contains RegistryDataMigrator.