Replace only on the first matching line with sed

bsdosxregular expressionsedtext processing

Using BSD sed (no GNU extensions), how can I perform an operation similar to the example provided below, but where instead of the line number, the replacement is performed on the first line in which a pattern occurs (rather than having to specify an actual number)?

Restricting to a line number

The simplest restriction is a line number.
If you wanted to delete the first number on line 3, just add a "3" before the command:

sed '3 s/[0-9][0-9]*//' <filename >newfilename

Source: http://www.grymoire.com/Unix/Sed.html#uh-26

Best Answer

{ sed -Ee'/[0-9]+/{s///;q;}'; cat; } <in >out

^should work w/ a BSD sed. but apparently it doesn't.

and so:

sed -e'/[0-9][0-9]*/{s///;:b' -e'n;bb' -e\} <in >out

...should work with any of them.

Related Question