Answers:
这是一个执行工作的perl单行程:
perl -ne 's/\s*\$\((\d+)\)\s*/"\n"." "x${1}/eg;print' file.txt
输出:
int main() {
return 0;
}
根据评论编辑:
perl -ne 's/\s*\$\((\d+)\)\h*(\R)?/"\n"." "x$1.$2/eg;print' file.txt
输入文件:
int main() { $(3) return 0; $(0) } $(0)
int main() { $(3) return 0; $(0) } $(0)
输出:
int main() {
return 0;
}
int main() {
return 0;
}
说明:
s/ : substitute
\s* : 0 or more spaces
\$\( : literally $(
(\d+) : group 1, 1 or more digits
\) : literally )
\h* : 0 or more horizontal spaces
(\R)? : group 2, optional, any kind of linebreak
/
"\n" : a linebreak
. : concatenate with
" "x$1 : a space that occurs $1 times, $1 is the content of group 1 (ie. the number inside parenthesis)
. : concatenate with
$2 : group 2, linebreak if it exists
/eg : flag execute & global
x
运营商
int main() { $(3) return 0; $(0) } $(0)
,我希望尾随$(0)
在两行之间引入一个空行,但事实并非如此。这是为什么?