Berkeley CSUA MOTD:Entry 26560
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2002/11/15-16 [Computer/SW/Languages/Perl, Computer/SW/Unix] UID:26560 Activity:moderate
11/15   What's the best way to print out a certain line number in a file?
        The best I could come up with is something like:
                head -(line # you want) filename | tail -1
        \_ Actually, i think ED is the answer
           \_ close.
           \_ echo number|ed filename
              But it will print the number of bytes in the file when
              ed starts up.  ED is the STANDARD!  Text processor. -geordan
        \_ sed -n <line#>p < filename
           \_ thx!
              \- hola, i think sed is a better tool than pl in this case
                 although a speed check would be interesting.
                 more generally sed -n 'X,Yp' will print out the inclusive
                 range from [X-Y]. On a big file this may be slightly better
                 although perhaps not a real big deal on mondern machines:
                 sed 'Xq;d' ... you do get more "sed intimidation points"
                 for the latter as well. --psb
                 \- i ran some tests on a sun blade 1000 lookin for a pretty
                    deep line on a medium big file ... about 60million lines.
                    here are the relative speeds:
                    wc -l: 1
                    sed: 1.03
                    perl4: 1.59
                    perl5: 3.85
                    stragely some old intuition about sed doesnt seem to
                    apply any more ... maybe it is because memory has
                    grown so much compared to typical files. --psb
        \_ You *know* there's a 1 line perl answer to this....
           \_ perl -ne 'print if $.==number' filename
              -geordan
           \_ ah! but where is the ocaml answer for this?