开发者

sed script to convert wikipedia links

开发者 https://www.devze.com 2023-02-25 17:19 出处:网络
Input: [[target|visible]] Output: visible Here\'s my try: \'s:\\[\\[\\[^\\|]*|\\(.*\\)]]:\\1:g\' (unesacped an开发者_JAVA技巧d spaced for readability:
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
0

精彩评论

暂无评论...
验证码 换一张
取 消