Here, the sed way
sed -E ':a;N;$!ba;s#a#Z#;s#a#Z#;s#a#Z#' alphabet
Since sed normaly works on lines, any command to sed will act only on 1 line at a time. To be able to replace only the first 3 occurances we need to first make the whole file a single selection on which we will do our 3 replacements. Otherwise we will do 3 replacements on each line.
:a
creates a label
N
appends next line into patterns space
$!
skips last newline
ba
branches to label a
We have now selected the whole file and will be acting on that space instead on one line at a time, do 3 replacements of "a" with "Z".
The above command will only work on GNU sed, more general but a bit uglier version that should work on non-GNU sed:
sed -e ':a' -e 'N' -e '$!ba' -e 's#a#Z#' -e 's#a#Z#' -e 's#a#Z#' alphabet
EDIT:
As suggested in the comments, adding version which uses g command to first replace all occurrences of 'a' with 'Z' and then replace all occurrences of 'Z' after 3th with 'a' again, which effectively leads to replacing only the first 3 occurrences of 'a'. This way you can change the last number to reflect the number of substitutions you need.
sed -e ':a;N;$!ba;s#a#Z#g;s#Z#a#g4' alphabet
While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill – 2015-12-12T00:22:29.100
1It's not very complicated code. Do you have any particular questions? – glenn jackman – 2015-12-12T04:50:51.807
No, but future readers of the answer might ... – DavidPostill – 2015-12-12T08:16:44.540
1Note that this will find only occurrences of
a
that are separate words. While this is true of the example data, it is not specified as being true of the real data. Also, if this changes a line that contains strings of multiple spaces (e.g.,c a t d o g
), it will cause those spaces to collapse to single ones (c Z t d o g
). – G-Man Says 'Reinstate Monica' – 2015-12-12T21:56:50.737