此 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);
}