ambikanair
ambikanair

Reputation: 4590

Remove a pattern using sed which has square brackets and quotes

How to remove a line which has this pattern using sed

resourceNames: [""]

I tried

sed -i '/resourceNames: [""]/d' ./sa.yaml

A part of file looks like this

- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: [""]
  verbs: ["get"]
---

Upvotes: 4

Views: 849

Answers (2)

Allan
Allan

Reputation: 12438

A better and more robust regex for your sed command:

sed -i '/\s*resourceNames:\s*\[""\]/d' ./sa.yaml

where you just escape the [] to avoid them being interpreted as character range, character list also, instead of space it is better to use \s*

Upvotes: 1

ambikanair
ambikanair

Reputation: 4590

Alright.I found the issue in my code. This worked

 sed -i '/  resourceNames: [[]""[]]/d' ./sa.yaml

Upvotes: 2

Related Questions