Mert Nuhoglu
Mert Nuhoglu

Reputation: 10143

Multiple substitute commands when matching some lines in sed

With Vim's global command, it is possible to chain multiple commands with | (pipe) symbol when matching some lines, for example:

g/match/ s/11/00/ | s/22/11/g

Is this also possible with sed without repeating the match regex?

sed -e '/match/ s/11/00/ ; /match/ s/22/11/g ' $file

If not, is it possible to do this with perl?

Upvotes: 1

Views: 682

Answers (1)

Lars Fischer
Lars Fischer

Reputation: 10189

You could use:

echo "->11,22<-
->01,20<-" | sed '/11/ {s/11/00/g; s/22/11/g}'

Output is:

->00,11<-
->01,20<-
  • the /11/ restricts the s commands inside its { ... } block only to matching lines

Upvotes: 2

Related Questions