home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15965 < prev    next >
Encoding:
Text File  |  1993-01-27  |  3.0 KB  |  107 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!destroyer!cs.ubc.ca!unixg.ubc.ca!kakwa.ucs.ualberta.ca!atlantis!aaron
  3. From: aaron@atlantis.uucp
  4. Subject: Re: Getting the nth line of a file
  5. Message-ID: <1993Jan26.151133.18029@atlantis.uucp>
  6. Organization: Atlantis Communications, Edmonton, AB, Canada
  7. X-Newsreader: TIN [version 1.1 PL8]
  8. References: <1993Jan25.163208.11697@atlantis.uucp>
  9. Date: Tue, 26 Jan 1993 15:11:33 GMT
  10. Lines: 95
  11.  
  12. I wrote:
  13. : Is there an easy, elegant, and quick way to get the Nth line of a file in
  14. : UNIX?  Or, failing that, in C?
  15.  
  16. : Currently I use the simple, but somewhat slow:
  17.  
  18. : head -n | tail -1
  19.  
  20. I received several email replies, which I will summarize here.
  21.  
  22.  
  23. rouben@math9.math.umbc.edu (Rouben Rostamian)
  24.  
  25. > To list the sixth line of a file, use:
  26.  
  27. > awk '{NR==6 {print; exit}' filename
  28.  
  29.  
  30. Unfortunately, I couldn't get this one to work.  But then, I'm not familiar
  31. with awk.  Perhaps people who are familiar with it can correct the above.
  32.  
  33.  
  34. Mark Borges <mdb@noaacrd.Colorado.EDU>
  35.  
  36. > I think the following may be faster and is equivalent:
  37.  
  38. > sed -n 'N p' file
  39.  
  40. > where -n supresses echoing the file to stdout, N is the Nth line you
  41. > want, p means to print it, and file is the input file. I'm not sure if
  42. > sed quits after printing the Nth line, which is really what you want
  43. > for speed; the sed man page always confuses me.
  44.  
  45. > Hope this works fast enough.
  46. > Mark
  47.  
  48. This one works just fine, and fast enough for my purposes.
  49.  
  50.  
  51. Thayne Forbes <thayne@unislc.slc.unisys.com>
  52.  
  53. > Ack! Barf!
  54. > Try this.
  55.  
  56. >     FLINE=`sed -n "$COUNT p" <$FILTER`
  57. > This is from my news filter program.  COUNT is the line number, and FILTER
  58. > is the file I am reading.  This reads the one line and assignes it to
  59. > FLINE.
  60.  
  61. > Remember, sed is your fiend!
  62.  
  63. Yes, I know that sed is my friend, and one day I'll have to get into it more
  64. seriously.  The above also works.
  65.  
  66.  
  67. ptsfa!dmturne@ns.PacBell.COM
  68.  
  69. > Try sed(1):
  70.  
  71. >     sed -n Np
  72.  
  73. > where N is the desired line number.
  74.  
  75. > For example, the third line of /etc/group would be:
  76.  
  77. >     sed -n 3p /etc/group
  78.  
  79. > Dave Turner    (510) 823-2001    {att,bellcore,sun,ames,decwrl}!pacbell!dmturne
  80.  
  81. This looks the most promising, being a direct statement with no quotes and no
  82. redirection, which fits my definition of elegant.  (So sue me, I want it to
  83. look nice as well as work.)
  84.  
  85.  
  86. Chris Sherman <sherman@unx.sas.com>
  87.  
  88. > Maybe:
  89.  
  90. > sed -n '30 {p;}' < /etc/passwd
  91.  
  92. > Will show the 30th line in /etc/passwd.
  93.  
  94. This also works.
  95.  
  96. So it seems that sed is the way to go, and some combination of the line number
  97. followed by p.  Since I'm calling this from a C program where I write to a
  98. string and then send it to system(3), it's convenient to use Dave Turner's
  99. method, though I imagine others might work better in scripts.  Thank you all
  100. for your help.
  101.  
  102. -- 
  103. ---Alfvaen(Still looking for "October's Baby")
  104. "Clocks don't bring tomorrow--knives don't bring good news."                       ---Bruce Cockburn
  105. Current Album--Mike Oldfield:The Killing Fields Soundtrack
  106. Current Read--Sean Russell:The Initiate Brother
  107.