CHESTERTOWN, N.Y. — At the Tops Market on Main Street recently, there was a "Mega Meat Sale" — buy one pork chop and get another free.

There were specials on avocados, paper towels and fried fish. Craft beer shared shelf space in the brightly lit store with cases of Genesee, a local favorite.

Tops was cutting prices even though it had filed for bankruptcy last month, responding to pressure from behemoths like Amazon and Walmart — which are lowering prices and targeting new markets — and from discount stores like Dollar General. The food war that is raging across the country is weeding out the weakest links, leaving small and medium-size grocery companies struggling to stay afloat.

More from The New York Times:

Remington, Centuries-Old Gun Maker, Files for Bankruptcy as Sales Slow

Trump's Tariffs Keep Allies, Markets and Industry Guessing

Uber to Sell Its Southeast Asia Business to Grab, a Regional Rival

It wasn't long before another wobbly chain followed Tops into bankruptcy. The parent company of the Southern stores Winn-Dixie and Bi-Lo said it would file for Chapter 11 protection by the end of this month, and close 94 stores.

"There is a tremendous shakeout in food retail right now," said Burt P. Flickinger III, a managing director of the retail consulting firm Strategic Resource Group, whose family founded a grocery business more than a century ago.

Amazon's $13 billion purchase of Whole Foods in June added a sense of urgency to the competition to feed American families, raising the prospect that the e-commerce giant would upend groceries just as it has every other aspect of retail. This month, Walmart responded with its own plan to start offering an online grocery delivery service in 100 cities.

At stake is not only the price of toothpaste and bananas, but the fate of thousands of cashiers, cake decorators and meat cutters, many of whom belong to labor unions and are owed pensions when they retire. Tops employs more than 12,000 unionized employees at about 160 stores in New York, Pennsylvania and Vermont.

Maribeth Druse made a lifelong career in groceries, but given the industry's struggles, her experience will increasingly be harder to replicate.

Ms. Druse, 61, was still in high school when she started working in the meat department of a grocery chain that Tops eventually acquired. She now collects a $20,000-a year-pension and is still able to work part time at the Tops in Cooperstown, N.Y.

"Who works in the same job for 44 years and gets a pension anymore?" Ms. Druse asked. "Nobody."

Like businesses in other industries — including Toys "R" Us, which announced liquidation plans this month — many failing supermarkets are owned by private equity firms that have loaded the companies up with debt. That hampers their ability to compete in an environment where prices in some markets have dropped by as much as 25 percent, Mr. Flickinger said.

Tops was long challenged by the debt its former private equity backer, Morgan Stanley Investment Management, heaped on it.

The private equity firm Lone Star has cashed out $980 million in dividends from Winn-Dixie's parent company since 2011, according to Moody's Investors Service. Most of the payments were made by taking out debt on the chain, leaving less money to invest in stores.

Marsh Supermarkets, an Indianapolis regional grocer that had been backed by private equity, laid off more than 1,500 workers and required a federal takeover of its pension plan last year.

And Fairway, the iconic New York grocer that the Blackstone Group took ownership of after it went bankrupt in 2016, is still trying to distinguish itself in a crowded field, but reports making some progress on its turnaround.

This month, Fairway executives met with the company's roughly 3,500 workers, most of whom are unionized, to unveil a set of new initiatives — like investments in a new marketing campaign. It plans to emphasize the company's role in bringing new foods to market, as it did with Chobani yogurt and Cape Cod potato chips.

"I am here to announce that Fairway has bounced back," the chief executive, Abel Porter, told a group of cheering workers.

Blackstone has been making investments in Fairway, but company executives acknowledge that the grocer faces an uphill battle.

"It's not a level playing field," Mr. Porter said in an interview. "Competing against Amazon is like competing against the government or a military commissary."

Amid the intense competition, the number of supermarkets around the country increased from 2010 to 2015, but the number of supermarket operators declined slightly. Analysts and industry executives say the pace of consolidation is expected to accelerate.

The changes are also taking a toll on unions. Membership in United Food and Commercial Workers, the largest grocery union, has dropped more than 9 percent since 2002, to about 1.2 million, according to the Labor Department.

While some union officials cite factors like automation and state laws unfavorable to organized labor for the decline in membership, others blame the bankruptcies.

"The private equity owners try to drain every last ounce of blood from these companies," said John T. Niccollai, president of Local 464A of the U.F.C.W., which represents grocery workers in New York and New Jersey. "Their feeling is if it goes bankrupt, so be it."

Mr. Niccollai, a former butcher who went on to get a law degree, works out of a cavernous union office, near Paterson, N.J., that is a throwback to a different era. A worn red carpet covers the floor; photos of union officials greeting priests adorn the wood-paneled walls.

When Mr. Niccollai started working at the union in the late 1970s, the A & P grocery chain had about 7,000 stores. By the time A & P had filed for its second bankruptcy, in 2015, it was down to about 125. Mr. Niccollai found jobs elsewhere for 3,500 workers who had been displaced by the bankruptcy, but 1,500 of his members were out of work.

He recently added membership by organizing some of the warehouse workers at the Peapod grocery delivery service, but it is challenging when the industry is increasingly dominated by nonunion employers like Walmart and Amazon.

"We are fighting hard," Mr. Niccollai said.

Tops's path into bankruptcy was similar to that of other unionized grocery chains. The first Tops supermarket was opened in Niagara Falls in 1962 by an Italian immigrant. The company grew into a large regional player that bought up smaller grocers and discount food stores through the 1990s.

The international food giant Ahold acquired Tops in 2001. The company was sold to Morgan Stanley's private equity team six years later. Under the firm's ownership, Tops loaded up on debt and paid out roughly $300 million in dividends to its investors, according to Moody's.

Even though Morgan Stanley no longer owns the company, Tops never overcame the debt burden. And like other unionized supermarket chains, Tops has had to deal with steep pension expenses.

Tight on cash, the grocery chain has faced tough competition, even in more remote markets like Chestertown, a community of about 700 people in the Adirondacks.

Many residents said they drove 35 miles to shop for food at Walmart on the weekends but shopped at Tops, often paying higher prices, during the week because it was close to home.

But Tops's virtual monopoly in the town ended in August 2016 when Dollar General — a national discounting chain — opened a store in Chestertown. The Dollar General doesn't sell much fresh produce or meat, but it is siphoning off Tops customers with huge deals on other staples.

At $2.80, a gallon of whole milk at Dollar General costs about the same as a half-gallon at Tops.

Alex Colpas, 27, who works at a marina on a local lake, said he rarely shopped at Tops any longer because of the deals at Dollar General.

"You don't even need a grocery store, frankly, if you can find a better way to shop," Mr. Colpas said. "It's a relic."

When it filed for bankruptcy, Tops said it expected to operate "as normal'' throughout the bankruptcy, but union officials are bracing for closings.

"I have never seen a bankruptcy that doesn't lead to closing stores," said Frank DeRiso, president of U.F.C.W. Local 1, which represents Tops workers in New York.

Tops, analysts say, would face a difficult time waging a price war with Dollar General.

"Survival depends on your ability to weather pricing pressure over an extended period of time," said Mickey Chadha, a senior credit officer at Moody's. "The weak guys can't do that."