home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!paladin.american.edu!howland.reston.ans.net!spool.mu.edu!olivea!sgigate!odin!akshay.csd.sgi.com!vidya
- From: vidya@akshay.csd.sgi.com (Vidya Alankar)
- Newsgroups: comp.unix.questions
- Subject: Re: Getting the nth line of a file
- Message-ID: <1993Jan26.232708.10878@odin.corp.sgi.com>
- Date: 26 Jan 93 23:27:08 GMT
- References: <1993Jan25.163208.11697@atlantis.uucp> <1993Jan26.151133.18029@atlantis.uucp>
- Sender: news@odin.corp.sgi.com (Net News)
- Organization: Silicon Graphics, Inc.
- Lines: 103
- Nntp-Posting-Host: akshay.csd.sgi.com
-
- In article <1993Jan26.151133.18029@atlantis.uucp>, aaron@atlantis.uucp writes:
- |> 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
-
- awk '{ if ( 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.
-
- It does read the full file. I do not know enough about sed to say if they
- is a way to stop after the Nth line. The awk command above does just this.
-
- |>
- |> > 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
-