The supermarket operator Albertsons said Tuesday morning that it would buy the remnants of the Rite Aid drugstore chain. By the end of the day, Walmart saw its stock drop by more than 10 percent on weak profit.

Both can blame Amazon.

An old-line grocer, Albertsons is striving for reinvigoration in a retail landscape being upended by e-commerce competitors and discount food stores. But it can be difficult for any company, no matter how strategic its deal-making, to pull off a revival in this environment.

The retail stalwart, Walmart has been trying to build its online presence as it moves to fend off Amazon. But even deep pockets don’t guarantee success in an increasingly digital landscape.

Amazon is the omnipresent foe, in whatever realm that Albertsons or Walmart plays in. The e-commerce giant wants to dominate the grocery store business, while also eyeing the health care industry.

“It’s the battle of the old generation versus the new generation,” said Craig Johnson, president of Customer Growth Partners, a retail consulting firm. “And right now the companies that are gaining share is the new generation.”

The deal for Rite Aid puts Albertsons directly in the middle of two major pushes by Amazon.

Albertsons plans to rebrand its in-house pharmacies under the Rite Aid name, and to continue operating some stand-alone Rite Aid stores. The bet is that the increased foot traffic to the in-house pharmacies will bring more customers to Albertsons’ food aisles, where they will pick up a rib-eye steak or a bunch of bananas on their way out.

Last year, Amazon moved aggressively to extend its e-commerce edge to groceries, acquiring Whole Foods for $13.4 billion in a deal that greatly enhanced its ability to deliver fresh food to millions of American households.

Amazon’s entry into the grocery business could hurt Albertsons and similar chains whose greatest asset may be the convenient locations of their stores. If Amazon increases its home delivery of fresh food, that convenience may not be as appealing.

Conventional grocers like Albertsons are also feeling pressure from chains like Trader Joe’s, which is known for its affordable high-quality products, and Wegmans, a growing regional chain that has built a cultlike following.

“Grocery is being hit by multiple factors,” said Gerald Storch, the former chief executive of the retailer Hudson’s Bay.

The pharmacy business, too, is undergoing major changes, with the nation’s largest operators of drugstores pursuing mergers to control costs and increase profits.

In December, the drugstore giant CVS Health said it would merge with the health insurer Aetna in a $69 billion deal that could reshape the way health care is delivered in the United States. That deal came together after a federal judge blocked Aetna’s proposed merger with rival Humana last year.

Rite Aid had previously sought to bulk up by merging with Walgreens. Negotiations between two of the biggest drugstore chains in the United States ended after antitrust authorities indicated they were unlikely to approve the combination. Instead, Rite Aid agreed last year to sell 1,932 stores and three distribution centers to Walgreens for $4.38 billion.

Amazon’s specter also looms over the health care field. The company recently teamed up with Berkshire Hathaway and JPMorgan Chase to form an independent health care company to serve the three firms’ employees and potentially revamp the broader industry.

For Albertsons, the Rite Aid deal is just the latest step to draw in more customers.

Originally founded by Joe Albertson in Boise, Idaho, in 1939, Albertsons grew primarily across the West. In 2013, it was acquired by a group of investors led by the private equity firm Cerberus Capital Management for about $3 billion.

Its biggest deal came in 2014 when Cerberus agreed to buy Safeway, another grocery chain whose roots lie in Idaho, for $9.2 billion. A planned public offering of the company’s stock in 2015 never took place.

The Rite Aid deal provides Albertsons’ investors with a way of finally going public after investing billions of dollars. Under the deal’s terms, Rite Aid shareholders would get $1.83 in cash and one share of Albertsons stock, or 1.079 shares of Albertsons stock, for every 10 shares of Rite Aid they owned.

Walmart’s biggest challenges are tied to its own plans to compete with Amazon.

Walmart spent heavily acquiring online retailers like Bonobos, Jet.com and ModCloth. Investors have largely been willing to forgive the company’s softening profits as long as the investments resulted in increased sales.

But Wall Street is getting impatient. In announcing the fourth-quarter earnings on Tuesday, Walmart said its online sales had grown 23 percent in the United States during the three-month period that ended Jan. 31. That was less than half the rate of growth in each of the previous three quarters.

“Walmart got a late start to the internet game, and they are still suffering for it,” said Mr. Johnson, of Customer Growth Partners.