Upon realization of investing in non-compliant companies, the individual should seek forgiveness from Allah swt and resolving not to do it again. Scholars have advised that such shares be sold and the appreciation be given in charity while the capital is to be retained by the investor. Detailed fatwa is as per below link.
https://islamqa.info/en/answers/35726/how-can-he-get-rid-of-shares-in-riba-based-banks