如何在shell脚本中交换单词?

时间:2011-10-08 06:22:11

标签: linux shell sed

这是我的输入线
[[ref:start|Other config files, programs, and scripts]]

我需要将上面的行转换为
 "Other config files, programs, and scripts":ref/start

如何使用sed命令转换它?

1 个答案:

答案 0 :(得分:2)

写作比阅读更容易:)

$ echo "[[ref:start|Other config files, programs, and scripts]]" |\
  sed 's/\[\[\([^:]*\):\([^|]*\)|\(.*\)\]\]/"\3":\1\/\2/'
"Other config files, programs, and scripts":ref/start

我建议您阅读man sed以及GNU sed manual。它不是那么大,你实际上不需要一次阅读。在大多数情况下,知道s///命令,正则表达式语法和sed标志(最有用的是-n-i)就足够了。