I'm planning to travel alone, using buses/trains.
I would also like to spend few days every so often on the beach to rest... in both Peru and Chile.
Does it sound like a reasonable plan?
That's entirely possible.
Firstly, there are no trains. Seriously. Don't bother trying to find them. In Argentina there's one from Buenos Aires to Rosario, but the bus is cheaper AND faster. There are some tourist trains in North Argentina (near Salta) and yes, there is the train from Cusco in Peru to Aguas Calientes, but that's about it.
I'll discuss in a north-bound trip, you could simply reverse that if you wished.
Starting in Santiago (most don't go further south, but you may want to), you can head to the beach at Viña del Mar and the cool sea-side town of Valparaiso.
Then most people would head north to either Iquique for beach (24 hours by bus) on the coast, or inland to San Pedro de Atacama. I'd recommend this, because you're going to want to do the salt flats. You could go from Iquique to San Pedro as well (via Calama). Take a couple of days in this cool desert town to do some activities (see Craters of the Moon, Valley of Death and the like, do an astronomy tour etc) and acclimatise to the altitude. It's about 2400m, which is the start of where you need to acclimatise and from personal experience, please, do make sure you do this.
The Salar de Uyuni salt flats tour is usually about 3-4 days, and is a must if you're in the area. Seriously.
You'll end in Uyuni, Bolivia. From there you could check out the silver town of Potosi and the mines, or head straight to La Paz. You'll want 2-3 days here to do the Death Road (mountain bike the world's most dangerous road) and explore the city.
Then you'll catch a 5 hour bus to Puno in Peru, by Lake Titicaca. One day is enough here, really, but two if you want.
Then bus up to Cusco, Peru. You'll want 2 nights to acclimatise some more if you've not done it, but book your tour to Machu Picchu and get ready for awesomeness. If you're doing the Inca trail, it'll take 4 days, or you could take one of the other hikes (there are several) or even just take the train to Aguas Caliente and go up in the morning.
Arequipa is an overnight bus ride away from Cusco, and you can take 2-3 days doing an exploration tour of Colca Canyon and see the mighty condors.
You could get out to the coast, but most head directly on to Lima. If your bus goes through Nazca early morning, hop out, go do a 30 min flight over the Nazca lines, and then head back to the bus station and catch a late morning bus to Lima.
It's about 24 hours from Arequipa to Lima, from memory.
Lima is a nice, interesting city, but a lot of people claim they get bored after 2-3 days. Stay in the Miraflores area, really nice with a cliff view of the coast.
Next for a great beach break, you could stop in Mancora on the north coast. Again, about 24 hours from Lima. Amazing beach town, spectacular weather, people didn't want to leave when I was there.
You're wanting to fly out, I assume, so you'll either have to bus back to Lima, or maybe carry on up to the border and change buses, go to Quito in Ecuador, and fly out of there.
Over all, you may not like some of the suggestions I've given, or have better or different ideas, but 5 weeks is entirely possible. There'll be a lot of busing, but you can fit it in.
Mark covered all the basics and if you stick to the major tourist attractions you can make it in five weeks. Doing a round-trip from Lima south over into Bolivia and the Northern Chile sounds do-able.
One 'attraction' that Mark left out, is the Amazon rain-forest, which you can visit in both Peru and Bolivia. After the cold Andes, this may be a welcome change for a week.
To get there overland takes a while, especially in Peru from Lima. It's a bit easier in Bolivia and a popular place is 'Rurrenabaque' which can be reached by bus from La Paz (20h). You can also fly, but because the runway is just grass, the planes may not be able to land or take off for days after a lot of rain.
You wont do a boat-ride on the Amazon proper, but you can do trips into the rain forest and there are lots of animals to see.
Even with only five weeks you may get away without booking many trips in advance. Most popular excursions can be booked the day before, the Inka trail being the exception.