2

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 aren't any options to specify this kind of thing, so how would I do this?

skeggse
  • 6,103
  • 11
  • 57
  • 81

3 Answers3

8

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

$ fold file | wc -l
Paul R
  • 208,748
  • 37
  • 389
  • 560
1

This may do what you want:

sed -r 's,(.{80}),\1\n,g' filename | wc -l
Erik
  • 88,732
  • 13
  • 198
  • 189
0

While the fold answer best fits the unix way:

awk '{n += 1+int(length/80)} END {print n}' filename
glenn jackman
  • 238,783
  • 38
  • 220
  • 352