home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!destroyer!cs.ubc.ca!unixg.ubc.ca!kakwa.ucs.ualberta.ca!atlantis!aaron
- From: aaron@atlantis.uucp
- Subject: Re: Getting the nth line of a file
- Message-ID: <1993Jan26.151133.18029@atlantis.uucp>
- Organization: Atlantis Communications, Edmonton, AB, Canada
- X-Newsreader: TIN [version 1.1 PL8]
- References: <1993Jan25.163208.11697@atlantis.uucp>
- Date: Tue, 26 Jan 1993 15:11:33 GMT
- Lines: 95
-
- 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.
-
-
- rouben@math9.math.umbc.edu (Rouben Rostamian)
-
- > To list the sixth line of a file, use:
-
- > awk '{NR==6 {print; exit}' filename
-
-
- Unfortunately, I couldn't get this one to work. But then, I'm not familiar
- with awk. Perhaps people who are familiar with it can correct the above.
-
-
- Mark Borges <mdb@noaacrd.Colorado.EDU>
-
- > I think the following may be faster and is equivalent:
-
- > sed -n 'N p' file
-
- > where -n supresses echoing the file to stdout, N is the Nth line you
- > want, p means to print it, and file is the input file. I'm not sure if
- > sed quits after printing the Nth line, which is really what you want
- > for speed; the sed man page always confuses me.
-
- > Hope this works fast enough.
- > Mark
-
- This one works just fine, and fast enough for my purposes.
-
-
- Thayne Forbes <thayne@unislc.slc.unisys.com>
-
- > Ack! Barf!
- > Try this.
-
- > FLINE=`sed -n "$COUNT p" <$FILTER`
- > This is from my news filter program. COUNT is the line number, and FILTER
- > is the file I am reading. This reads the one line and assignes it to
- > FLINE.
-
- > Remember, sed is your fiend!
-
- Yes, I know that sed is my friend, and one day I'll have to get into it more
- seriously. The above also works.
-
-
- ptsfa!dmturne@ns.PacBell.COM
-
- > Try sed(1):
-
- > sed -n Np
-
- > where N is the desired line number.
-
- > For example, the third line of /etc/group would be:
-
- > sed -n 3p /etc/group
-
- > Dave Turner (510) 823-2001 {att,bellcore,sun,ames,decwrl}!pacbell!dmturne
-
- This looks the most promising, being a direct statement with no quotes and no
- redirection, which fits my definition of elegant. (So sue me, I want it to
- look nice as well as work.)
-
-
- Chris Sherman <sherman@unx.sas.com>
-
- > Maybe:
-
- > sed -n '30 {p;}' < /etc/passwd
-
- > Will show the 30th line in /etc/passwd.
-
- This also works.
-
- So it seems that sed is the way to go, and some combination of the line number
- followed by p. Since I'm calling this from a C program where I write to a
- string and then send it to system(3), it's convenient to use Dave Turner's
- method, though I imagine others might work better in scripts. Thank you all
- for your help.
-
- --
- ---Alfvaen(Still looking for "October's Baby")
- "Clocks don't bring tomorrow--knives don't bring good news." ---Bruce Cockburn
- Current Album--Mike Oldfield:The Killing Fields Soundtrack
- Current Read--Sean Russell:The Initiate Brother
-