Changing the NBT data of a variable amount of items in a chest inventory?

My goal is to be able to put a variable amount of gold nuggets in a chest and then run some commands to replace those with gold nuggets that have edited display NBT data.
Is this even possible? I'm encountering a few issues.
First, when checking for the amount of gold nuggets in an inventory with /testforblock, I have to specify the amount of gold nuggets I am testing for. However, I want the amount of nuggets you input to be variable, not restricted to a certain stack size. I also want it to be able to work with every slot in the chest, so somebody can fill the whole thing up.
Second, how would I store the amount of nuggets being inputted into the chest and pass that value to /replaceitem so that I replace the correct number of gold nuggets with edited display NBT nuggets?
Pictures about "Changing the NBT data of a variable amount of items in a chest inventory?"



How do you modify the NBT data of an entity?
You can use the /data command to get, merge and remove NBT tags (formerly called data tags) for entities and block entities. The /data command is most useful for changing attributes of a mob after the mob has spawned or modifying attributes of a block entity after you have placed it.How do I change my NBT tag in Minecraft?
The NBT tag is always surrounded in {} such as {NoAI:1}. If there is more than one NBT tag used in a command, the NBT tags are separated by a comma such as {NoAI:1, IsBaby:1}. Each entity (or mob) has its own unique set of NBT tags. Select an entity to see the NBT tags that you can change.How do you get the NBT data of a chest in Minecraft?
Look at the chest, hold ctrl , and press your pick block button (configurable in options). This should give you a chest item with identical NBT data.How do I send an item to NBT tag?
NBT tags are used to specify information for items and entities created with the " /give ", " /summon ", " /tellraw " (for the JSON message), " /fill ", " /blockdata {Until1.13}", " /setblock ", and " /clear " commands. Each Tag has the format <tagname>:<value> . Tag names are case-sensitive.Minecraft /data Command Tutorial
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Anete Lusina, Tiger Lily, Steve Johnson, Rachel Claire