Skip to main content
Remove "tag"-like syntax from title
Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

Sed: search Search and replace lines AFTER a regex match using "sed"

added 16 characters in body
Source Link
Daniel Kaplan
  • 1.1k
  • 3
  • 17
  • 30

This kind of feels like it would be easier in awk, but I'm curious if sed can do it. Here is my input:

line 1
line 2
line 3
line 1
line 2
line 3
line 1
line 2
line 3

I'd like to write an in-place regex that finds the second line 1, then replaces all line 3's found after that. The output would look like this:

line 1
line 2
line 3
line 1
line 2
replaced
line 1
line 2
replaced

I'm not really looking for clever"clever" solutions that only apply to this input. I'm asking this questionI want to learn if there is a general-purpose way to search and replace after a match with sed.

I thought the solution would be somewhere in the addr documentation, but it doesn't seem to describe /starting point/,s/... as something you can do, and I'm getting error when I attempt to do so.

This kind of feels like it would be easier in awk, but I'm curious if sed can do it. Here is my input:

line 1
line 2
line 3
line 1
line 2
line 3
line 1
line 2
line 3

I'd like to write an in-place regex that finds the second line 1, then replaces all line 3's found after that. The output would look like this:

line 1
line 2
line 3
line 1
line 2
replaced
line 1
line 2
replaced

I'm not really looking for clever solutions to this input. I'm asking this question to learn if there is a way to search and replace after a match with sed.

I thought the solution would be somewhere in the addr documentation, but it doesn't seem to describe /starting point/,s/... as something you can do, and I'm getting error when I attempt to do so.

This kind of feels like it would be easier in awk, but I'm curious if sed can do it. Here is my input:

line 1
line 2
line 3
line 1
line 2
line 3
line 1
line 2
line 3

I'd like to write an in-place regex that finds the second line 1, then replaces all line 3's found after that. The output would look like this:

line 1
line 2
line 3
line 1
line 2
replaced
line 1
line 2
replaced

I'm not really looking for "clever" solutions that only apply to this input. I want to learn if there is a general-purpose way to search and replace after a match with sed.

I thought the solution would be somewhere in the addr documentation, but it doesn't seem to describe /starting point/,s/... as something you can do, and I'm getting error when I attempt to do so.

edited tags
Link
Daniel Kaplan
  • 1.1k
  • 3
  • 17
  • 30
Source Link
Daniel Kaplan
  • 1.1k
  • 3
  • 17
  • 30
Loading