home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ftpbox!news.acns.nwu.edu!nucsrl!ddsw1!dattier
- From: dattier@ddsw1.mcs.com (DWT)
- Subject: Re: Getting the nth line of a file
- Message-ID: <C1Is53.4F2@ddsw1.mcs.com>
- Date: Wed, 27 Jan 1993 15:53:27 GMT
- References: <1993Jan25.163208.11697@atlantis.uucp> <1993Jan26.151133.18029@atlantis.uucp>
- Organization: Contributor Account at ddsw1, Chicago, Illinois 60657
- Lines: 39
-
- aaron@atlantis.uucp wrote in <1993Jan26.151133.18029@atlantis.uucp>:
-
- | I wrote:
- | : Is there an easy, elegant, and quick way to get the Nth line of a file in
- | : UNIX? Or, failing that, in C?
- |
- | : Currently I use the simple, but somewhat slow:
- |
- | : head -n | tail -1
- |
- | I received several email replies, which I will summarize here.
-
- ... etc.
-
- Several people suggested
-
- sed -n __p file
-
- where "__" is a decimal numeral for the desired line number. sed __!d file
- is equivalent (and shorter to write, but escape the bang if you use *csh).
-
- The following should be somewhat faster, as it quits after printing the __th
- line instead of reading and discarding the rest of the input:
-
- sed -n '__{
- p
- q
- }' file
-
- where again "__" is a numeral for the desired line number's position. You
- can put the numeral outside the quotes or use weak quotes instead in case
- you'd like to use a variable there.
-
- In some later seds it can be condensed to this:
-
- sed '__{p;q;}' file
-
- David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297
- dattier@ddsw1.mcs.com CompuServe: 73720,1570 MCI Mail: 426-1818
-