A different solution that is harder to set up, but requires fewer blocks, could look like this:
To replace a diamond in the middle slot with diamond ore you would use this command:
execute if block 18 67 -119 minecraft:dispenser{Items:[{Slot:4b,id:"minecraft:diamond",Count:1b}]} run setblock 18 67 -119 minecraft:dispenser[facing=north,triggered=true]{Items:[{Slot:4b,id:"minecraft:diamond_ore",Count:1b}]}
It is important that the triggered
value changes, because if it stays the same, the command will fail to put the diamond ore into the dispenser. This should not be a problem in practice though, but you should keep it in mind.
To craft a piston using this soultion you would use this command:
execute if block 18 67 -119 minecraft:dispenser{Items:[{Slot:0b,id:"minecraft:oak_planks",Count:1b},{Slot:1b,id:"minecraft:oak_planks",Count:1b},{Slot:2b,id:"minecraft:oak_planks",Count:1b},{Slot:3b,id:"minecraft:cobblestone",Count:1b},{Slot:4b,id:"minecraft:iron_ingot",Count:1b},{Slot:5b,id:"minecraft:cobblestone",Count:1b},{Slot:6b,id:"minecraft:cobblestone",Count:1b},{Slot:7b,id:"minecraft:redstone",Count:1b},{Slot:8b,id:"minecraft:cobblestone",Count:1b}]} run setblock 14 67 -119 minecraft:dispenser[facing=north]{Items:[{Slot:4b,id:"minecraft:piston",Count:1b}]}
The creaftin-table-dispenser would be at the coordinates 18 67 -119
and it should face north.
This command checks for exact values, so you cannot have more than 1 of the specified item in a slot, or it will not work (unless you set the count to something different).
Note: This command does NOT check if there are any other items in slots that are not used by the recipee, those will get deleted in the process