开发者

UNIX wc -l with line length restriction

I need to count the number of lines in a file, in a UNIX shell script, but I need the number of lines under 80 characters, and if there are more than 80 characters, count it as multiple lines.

I know wc -l counts the number of lines, and I know there 开发者_C百科aren't any options to specify this kind of thing, so how would I do this?


Use fold to break lines > 80 characters and then pipe the output to wc, e.g.

$ fold file | wc -l


This may do what you want:

sed -r 's,(.{80}),\1\n,g' filename | wc -l


While the fold answer best fits the unix way:

awk '{n += 1+int(length/80)} END {print n}' filename
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜