One option if you know with lines we’re used (there are sometimes many options from A to B) is to use Wikipedia and a calculator.
At least in France, but this is probably true elsewhere, Wikipedia has a page for each line which lists all stations and junctions along the line with the position of each station and junction on that line (in France it’s called a “pk”, as in “point kilométrique”).
So you take the position of the point you entered that line, the one where you exited that line, subtract and get the number of kilometres on that line. Then you add up all the segments.
In some cases an entire trip will use a single line so that’s easy. In others it can take many different lines and it’s going to be a bit more work. Using half a dozen different lines or more is far from uncommon.
In France if you want to have a rough idea you can also use the “Atlas du réseau ferré français” which has a detailed map of the whole French railway network with line numbers and rough distance indicators along each line (IIRC there’s a marker every 25 km)