home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 16005 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  3.7 KB

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