Bhabani Shankar
Bhabani Shankar

Reputation: 1285

Unix find and replace all with a condition

I have a condition where I want to find and replace all '/ ' (forward / with a space after that) in a file for some lines having a specific entries. Example below:

g aaa / cccc dd
k eee / hhhh dd
m aaa / kkkk ll

I want to replace '/ ' with '/' for the rows with 'aaa' entry.

Upvotes: 1

Views: 194

Answers (1)

xebtl
xebtl

Reputation: 490

Like this?

perl -pe '/aaa/ and s[/ ][/]'

Of course it can be refined in many ways, e.g., add g to the s[][] to replace all matches, add the -i switch to edit files “in place”, etc.

Upvotes: 2

Related Questions