How to histogram length of lines in a file?

How to histogram length of lines in a file?

Post by Stan » Thu, 04 Jul 1996 04:00:00



I want to intelligently set up enscript (with a font size,
with portrait or landscape, and with -2 or not) depending on
how long the lines are.  But I'm willing to accept some line
wraps, just not too many.  What's the best way to get some
output like:

  0:   50
  1:    5
  . . .
  46: 300
  . . .
  80:  23
  . . .
  110:  2

where the first number is the number of characters in the
line, and the second number is the number of lines in the
file with that line length?

How would you do this?  In awk, perl, or any other tool?

 
 
 

How to histogram length of lines in a file?

Post by Randal L. Schwar » Thu, 04 Jul 1996 04:00:00


Stan> I want to intelligently set up enscript (with a font size,
Stan> with portrait or landscape, and with -2 or not) depending on
Stan> how long the lines are.  But I'm willing to accept some line
Stan> wraps, just not too many.  What's the best way to get some
Stan> output like:

Stan>   0:   50
Stan>   1:    5
Stan>   . . .
Stan>   46: 300
Stan>   . . .
Stan>   80:  23
Stan>   . . .
Stan>   110:  2

Stan> where the first number is the number of characters in the
Stan> line, and the second number is the number of lines in the
Stan> file with that line length?

Stan> How would you do this?  In awk, perl, or any other tool?

        #!/usr/bin/perl
        $length{length}++ while <>;
        print map "$_: $length{$_}\n",
                sort { $a <=> $b } keys %length;

Or, in 1.5 lines:

perl -ne '$x{length}++;' \
        -e 'END {print map "$_: $x{$_}\n", sort { $a <=> $b } keys %x; }'

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $18,720.69 collected, $172,159.85 spent; just 789 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

How to histogram length of lines in a file?

Post by Harrison Berger » Fri, 05 Jul 1996 04:00:00


 >I want to intelligently set up enscript (with a font size,
 >with portrait or landscape, and with -2 or not) depending on
 >how long the lines are.  But I'm willing to accept some line
 >wraps, just not too many.  What's the best way to get some
 >output like:
 >
 >  0:   50
 >  1:    5
 >  . . .
 >  46: 300
 >  . . .
 >  80:  23
 >  . . .
 >  110:  2
 >
 >where the first number is the number of characters in the
 >line, and the second number is the number of lines in the
 >file with that line length?
 >
 >How would you do this?  In awk, perl, or any other tool?

#!/bin/sh

awk '{++line[length($0)]}
END{
        for (var in line)
                printf "%d : %d\n", var, line[var]

Quote:}' $1 |sort

--

    Steinberger:
State of the Instrument
 
 
 

How to histogram length of lines in a file?

Post by Bill Marc » Fri, 05 Jul 1996 04:00:00



>where the first number is the number of characters in the
>line, and the second number is the number of lines in the
>file with that line length?

>How would you do this?  In awk, perl, or any other tool?

awk '{lengths[length]++;if(length>maxlen)maxlen=length}; END{
for(i=1;i<=maxlen;i++) if(i in lengths)print i ": "length(i)}

--

On 22 July, 1996, at 6:00 pm GMT, everyone in the world    
just START HUMMING.  Those who don't know will freak.      

 
 
 

1. Group File Line Length Exceeded

We have system with lots of users (2500+), and we run into problems using
mkuser under AIX 4.3.3 because it adds the user to the group file, eventually exceeding
the line length limit. When this happens, people can't log in, crons
fail, etc., because the gorup file is no longer readable.

Is there some way to modify the behavior of the mkuser command so that it
won't add the user to the group file? Since their primary group is
already established in the passwd file, I don't see where this is even
necessary. I've seen quite a few posts related to this topic, but I've
yet to find the fix. Does AIX 4.3.3 have a way to control this, or should
I just come up with a replacement for the mkuser command?

2. TWo NETWORKS

3. Line Length Limit in Netgroup file?

4. modem overflow?

5. find length longest line in a file

6. LILO question

7. Lengths of the sequences of common lines in two files

8. FS: 533 MHz Alpha 4 MB cache with 512 MB RAM...

9. a SED script to find length of the longest line in a file

10. .htgroup files and max line length

11. Getting line lengths from a file

12. print line 1 of each file, line 2 of each file...

13. how to append one file's line to another file's line?