這awk腳本將在最後一個逗號最大線長,之前分割你的線,如果你喜歡,你可以設置爲運行時參數。它使分割線縮進比原始線多4個空格:
$ cat file
function xyz()
{
somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5, $somevariable6, $somevariable7, $somevariable8, $somevariable9);
somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5, $somevariable6, $somevariable7, $somevariable8, $somevariable9);
}
$ awk -f tst.awk file
function xyz()
{
somecall($somevariable1, $somevariable2, $somevariable3,
$somevariable4, $somevariable5, $somevariable6,
$somevariable7, $somevariable8,
$somevariable9);
somecall($somevariable1, $somevariable2, $somevariable3,
$somevariable4, $somevariable5, $somevariable6,
$somevariable7, $somevariable8,
$somevariable9);
}
$
$ cat tst.awk
BEGIN{ maxLength = (maxLength ? maxLength : 66) }
(length($0) > maxLength) && /,/ {
indent = $0
sub(/[^[:space:]].*/,"",indent)
tail = $0
while (tail ~ /[^[:space:]]/) {
head = substr(tail,1,maxLength)
sub(/,[^,]+$/,",",head)
tail = substr(tail,length(head)+1)
sub(/^[[:space:]]*/,indent" ",tail)
print head
}
next
}
1
$ awk -v maxLength=100 -f tst.awk file
function xyz()
{
somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5,
$somevariable6, $somevariable7, $somevariable8,
$somevariable9);
somecall($somevariable1, $somevariable2, $somevariable3, $somevariable4, $somevariable5,
$somevariable6, $somevariable7, $somevariable8,
$somevariable9);
}
$ awk -v maxLength=30 -f tst.awk file
function xyz()
{
somecall($somevariable1,
$somevariable2,
$somevariable3,
$somevariable4,
$somevariable5,
$somevariable6,
$somevariable7,
$somevariable8,
$somevariable9);
somecall($somevariable1,
$somevariable2,
$somevariable3,
$somevariable4,
$somevariable5,
$somevariable6,
$somevariable7,
$somevariable8,
$somevariable9);
}
謝謝,這完全是我需要的。 – 2013-03-22 09:18:24