A man was savagely beaten with a bike lock in a random attack just outside the landmark New York Public Library building Friday afternoon before he was saved by two good Samaritans, witnesses and authorities said.

Paul Shaw, 64, of the Upper West Side, was on the bustling sidewalk at 42nd Street and Fifth Avenue near Bryant Park at about 2:20 p.m. when the thug pummeled him in front of stunned onlookers, according to law-enforcement sources.

The suspect, identified by sources as 27-year-old David Aleer-Chol, “grabbed a bike lock [off his bike] and hit him on the head,” said a vendor at a nearby Nathan’s food cart.

“I went outside to try to stop him. He said ‘go back or I’ll f–k you up, too,’ ” said the ­Samaritan, who asked that he not be identified.

“He hit him twice. [The victim] fell down. The attacker backed up and hit him four or five more times when he didn’t die,” the witness said.

The vendor said that he stepped in after the first two blows.

“I’m pretty sure if I didn’t stop him, he was going to kill him,” said the food purveyor, who added that at some point during the merciless assault, a bystander called 911.

The vendor said the suspect is a deliveryman in the area.

A woman also intervened — using her body as a shield between the attacker and the ­victim, who was bleeding from his head.

“Stop it! Stop it!” the woman could be heard shouting before the brute took off, witnesses said.

Aleer-Chol was arrested minutes later a block away and charged with assault, police said.

Emergency responders rushed the blood-soaked victim to Bellevue Hospital, where he was in serious condition.

A motive for the vicious attack was not immediately known.

Police said a 911 caller reported that it looked like the victim was being attacked with a belt.

Law-enforcement sources confirmed that Shaw was hit with a bike lock.

In 2015, a madman with a machete in Bryant Park slashed a tourist from South Korea in broad daylight as she was leaving a yoga class inside the greenspace.

The attacker, Frederick Young, 45, who copped to first-degree assault as part of a plea deal, was sentenced in May of last year to 12 years in prison.

Young has at least two dozen prior arrests on his record, including a 2010 machete attack.

Additional reporting by Stephanie Pagones and Liz Pressman