You could do this by writing a [function][1] (*.mcfunction* file) and placing it in a datapack in the map folder. To create a function, follow these steps: 

 1. Open notepad (or your text editor of choice), and enter the commands you wish to run, each on their own line.
 2. Save the file, name it whatever you want, (replace *.txt* with *.mcfunction*) on your desktop.
 3. Create folders in this order:
    
    ```datapacks / [name of datapack] / data / [namespace] / functions```

 4. Move the *.mcfunction* file you created into the functions folder, and move the datapacks folder into your world folder. (for a Realm, you will first need to download the map - go to World backups and Download latest) 

 5. (for a Realm) Upload the modified map to the Realm.

 6. Run the function with ```/function [namespace] : [function name]```. This can be done by a player or from a command block.

*Note: @p selector will be relative to the command block or player that ran the function, in this usecase I'm assuming @a would fit better.*


  [1]: https://minecraft.wiki/w/Function_(Java_Edition)