sed script to convert wikipedia links
Input: [[target|visible]]
Output: visible
Here's my try:
's:\[\[\[^\|]*|\(.*\)]]:\1:g'
(unesacped an开发者_JAVA技巧d spaced for readability:
's: [[ [^\|]* | (.*) ]]:\1:g'
)
But it doesn't work.
EDIT: Solved it:
's:\[\[[^]\|]*\|\([^]]*\)]]:\1:g'
You could use extended regular expressions with sed -r
:
echo '[[target|visible]]' | sed -r 's:^\[\[[^\|]*\|(.*)\]\]$:\1:'
I didn't find out how to match |
with basic regexps...
Try awk, its more suited for this job. Split on "|" and then get the 2nd field. Simple as that. Remove those ]
as desired
$ echo "[[target|visible]]" | awk -F'|' '{gsub(/\]/,"",$2);print $2}'
visible
or any tools that does splitting of strings easily. eg Ruby(1.9+)
$ echo "[[target|visible]]" | ruby -e 'puts gets.split("|")[-1].gsub(/\]/,"")'
visible
精彩评论