You can start recording a keyboard macro by typing:
Do whatever you want and stop recording with:
Then, you can select a region and type:
C-x C-k r
To apply the macro to the selected region.
Emacs Macros are a really powerful tool. Just think if you need to prepone/postpone a string to a group of lines, for example from: