Yasas
Yasas

Reputation: 73

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?

Upvotes: 1

Views: 53

Answers (2)

potong
potong

Reputation: 58420

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.

Upvotes: 2

Yasas
Yasas

Reputation: 73

I just found this answer from

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

And it works for me!

Upvotes: 3

Related Questions