I think I would use Perl. This can be done as a single line, thus:
perl -pe 's{\[\[([^/.|]+)(|[^]]+)?\]\]}{$x=$1;$y=$2;$x=~s%_%/%g;"[[$x$y]]"}gex;' <<'EOF' this is a line with a [[Link_to_something]] and [[Something_else|something else]] this site is cool [[http://example.com/this_page]] EOF
The way out of this:
this is a line with a [[Link/to/something]] and [[Something/else|something else]] this site is cool [[http://example.com/this_page]]
Is this a good style etc. completely open to discussion.
I will explain this version of the code, which is isomorphic to the code above:
perl -e 'use strict; use warnings; while (my $line = <>) { $line =~ s{ \[\[ ([^/.|]+) (|[^]]+)? \]\] } { my($x, $y) = ($1, $2); $x =~ s%_%/%g; "[[$x$y]]" }gex; print $line; } '
The while basically matches -p in the first version. I explicitly named the input variable as $line instead of using the implicit $_ , as in the first version. I also had to declare $x and $y due to use strict; use warnings; use strict; use warnings; .
Command substitution takes the form s{pattern}{replace} because the regular expressions themselves have slashes. The modifier x allows (non-essential) spaces in two parts, which simplifies its calculation. The g modifier repeats the replacement as often as the pattern matches. The e modifier says: "Treat the right side of the lookup as an expression."
The corresponding pattern looks for a pair of open square brackets, and then remembers a sequence of characters other than / ,. or | optionally followed by | and a sequence of characters other than ] , ending in a pair of close square brackets. Two captures: $1 and $2 .
The replacement expression stores the values โโof $1 and $2 in the variables $x and $y . He then applies a simpler replacement to $x , changing the underscores to slashes. Then the result is the string [[$x$y]] . You cannot change $1 or $2 directly in a replacement expression. And the internal clobbers are $1 and $2 , so I needed $x and $y .
Perhaps there is another way to do this - this is Perl, so TMTOWTDI: there is more than one way to do this. But that at least works.