replace nth occurence of string in each line of a text file
You can add a number at the end of the substitute command. For example, the following will substitute the second occurrence of old
with the string new
on each line of file
:
sed 's/old/new/2' file
So, instead of your proposed solution, you can use:
sed 's/ /|/2'
For more information, see e.g. this sed tutorial.