Did the brothers ever realize the gravity of their sin when they sold Joseph?
Sforno comments that after their detainment in Egypt, the brothers examined their deeds to see why God had punished them. They do not display any remorse for the sale itself, "Indeed we are guilty concerning our brother inasmuch as we saw his heartfelt anguish when he pleaded with us and we paid no heed; that is why this anguish has come upon us," (Mikeitz 42:21). It seems that the brothers saw the sale as a harsh act, albeit the correct one.
How did the brothers rationalize this?
One interesting possibility is the brothers felt that Joseph wasn't a threat to them, but to their family's destiny. They were fully aware of the 'weeding out' process which had seen Ishmael and Esau 'removed' from the nation of Israel. The brothers saw that Joseph was bringing dissension to the family, which would put a dent in the mission of the Patriarchs. Hence, he was a danger that had to be dealt with.
(Ideas taken from The Stone Edition Chumash)