在vim / bash中复制一行的几个部分

时间:2014-11-03 10:06:23

标签: bash vim

我需要在vim / bash中更改给定格式的多个字符串:
实际的i / p:%localdate%,%localtime%,%uptime%
期望的o / p:localdate=%localdate%,localtime=%localtime%,uptime=%uptime%

有100个这样的变量需要在文件中更改。请用bash / vim建议这种方法。

2 个答案:

答案 0 :(得分:2)

你可以试试这种方式

:%s/\(%\(\w*\)%\)/\2=\1/g

说明:

\(%\(\w*\)%\)   -- Grouping the pattern  "%localdate%"
(\w*\)          -- Grouping the pattern  between the %string% line "localdate" only
\2=\1           -- Print the matched pattern  group 2 and group 1 like "localdate=%localdate%"

输出

localdate=%localdate%,localtime=%localtime%,uptime=%uptime%

答案 1 :(得分:0)

尝试:

awk -F',' '{for (i=1;i<=NF;i++) {myvar=substr($i, 2, length($i)-2); printf("%s=%s,", myvar,$i)}print ""}' test.txt | sed 's/,$//'