4

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

  • Please edit your question and give us an example input and the output you would like to see. It seems like you are just asking how you can replace only the first occurrence of a string in a file but it's hard to understand without an example. – terdon Dec 20 '15 at 22:59
  • @terdon "It seems like you are just asking how you can replace only the first occurrence of a string in a file.." Yes, exactly. That's correct. – tjt263 Dec 20 '15 at 23:05
  • OK. So, please edit your question and add an example input and the output you would like to see with it. – terdon Dec 20 '15 at 23:09
  • 2
    That's question 4.11 of the sed FAQ – Stéphane Chazelas Dec 20 '15 at 23:22
1
{ 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.

  • 1
    Doesn't work on FreeBSD 10 even on seekable input. On FreeBSD, you can run sed under stdbuf -i 1 to work around it (would read the input one byte at a time though) – Stéphane Chazelas Dec 20 '15 at 23:33
  • @StéphaneChazelas - weird. should work on a Mac, though. I would think - it has the stamp. – mikeserv Dec 20 '15 at 23:40
  • 1
    To clarify, now that the answer was edited with a second, portable solution, it's the first one that doesn't work on FreeBSD 10 (though should on seekable files if FreeBSD were POSIX (q leaving the cursor in the right place)), the second one is even better than the solution given in the FAQ as the FAQ one uses N instead of n (which would slurp the rest of the file in the pattern space). – Stéphane Chazelas Dec 20 '15 at 23:55
  • @StéphaneChazelas - I noticed that as well, which is why I didn't delete it - though I thought at first I would. thanks again, sc. – mikeserv Dec 21 '15 at 0:01
0

This works perfectly:

sed '1,/abc/s/abc/xyz/' file.txt

Can be abbreviated to:

sed '1,/abc/s//xyz/' file.txt

Compatible with GNU & BSD:

sed '1,/[0-9][0-9]*/s///' <filename >newfilename
  • 2
    That doesn't work (could run that s command on more than one line) if the first match is on the first line (as noted in the FAQ) – Stéphane Chazelas Dec 21 '15 at 11:08
  • (Yeah, okay, we get it - You've read the FAQ.) If theres a match on the first line; the question is irrelevant, so thats fine. – tjt263 Jan 4 '16 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.