home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13816 < prev    next >
Encoding:
Text File  |  1992-11-22  |  2.8 KB  |  77 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!att!princeton!fish.Princeton.EDU!lhjensen
  3. From: lhjensen@fish.Princeton.EDU (Leif Jensen)
  4. Subject: Re: How do I get a particualr line of a file?
  5. Message-ID: <1992Nov23.010921.15421@Princeton.EDU>
  6. Originator: news@nimaster
  7. Sender: news@Princeton.EDU (USENET News System)
  8. Nntp-Posting-Host: fish.princeton.edu
  9. Organization: Princeton University
  10. References: <pec2.722467852@Isis.MsState.Edu> <1ep3j0INNnvk@clover.csv.warwick.ac.uk>
  11. Date: Mon, 23 Nov 1992 01:09:21 GMT
  12. Lines: 63
  13.  
  14. In article <pec2.722467852@Isis.MsState.Edu> pec2@ra.msstate.edu (Paul E. Carroll) writes:
  15. >This seems extremely simple, but I've run into the problem several times
  16. >and gotten stumped each time.  So how do I view a particualr line of a 
  17. >file?  Head looks at the top, tail the end, cat - echo - more - or less-
  18. >look at the whole thing... but what if I want to see just line 30?
  19. >or JUST line 210?  I'm stumped.
  20.  
  21. There are lots of ways to do this.  Some are better than others.  Since
  22. there are so many correct answers, I think that for efficiency's sake, 
  23. it is important to use a method that does not read more of the file
  24. than it has to. Here's a short list.
  25.  
  26. sed -n '200{;p;q;}' /etc/termcap
  27. awk 'NR==200 {print;exit}' /etc/termcap
  28. sed 200q /etc/termcap | tail -1
  29. head -200 /etc/termcap | tail -1
  30. perl -ne 'if ($. == 200) {print; exit}' /etc/termcap
  31.  
  32. In article <1ep3j0INNnvk@clover.csv.warwick.ac.uk> maupb@csv.warwick.ac.uk (Mr J L Saunders) writes:
  33. >Have a look at the manual for awk...
  34. >
  35. >awk '{if (NR == line) print}' line=linenumber filename
  36. >
  37. >Where linenumber is the line you want, and filename is the file you're looking
  38. >at. eg. 
  39. >
  40. >>awk '{if (NR == line) print}' line=5000 /usr/dict/words
  41. >concrete
  42.  
  43. I don't know why you would use the if(...) method to choose the line.
  44. The awk command I list above seems much more intuitive.  Your solution
  45. also suffers from reading much more of the file than it has to.  Even
  46. on a fast sun workstation with no other users, the effect is dramatic:
  47.  
  48. % ls -l /tmp/foo
  49. -rw-------  1 lhjensen  3138380 Nov 22 19:46 /tmp/foo
  50. % time awk '{if (NR == line) print}' line=200 /tmp/foo
  51. hn|2621-nl|hp2621nl|2621nl|hp2621-nl|hp 2621 with no labels:\
  52. 22.810u 0.780s 0:23.72 99.4% 0+162k 0+0io 0pf+0w
  53. %
  54.  
  55. In article <1992Nov22.235943.13501@umbc3.umbc.edu> rouben@math9.math.umbc.edu (Rouben Rostamian) writes:
  56. >To print line 30:
  57. >sed -n 30p filename
  58. >
  59. >To print lines 30 to 35:
  60. >sed -n 30,35p filename
  61.  
  62. Ditto for this command.  The relevant times are
  63.  
  64. % time sed -n 200p /tmp/foo
  65. hn|2621-nl|hp2621nl|2621nl|hp2621-nl|hp 2621 with no labels:\
  66. 2.810u 1.070s 0:03.96 97.9% 0+114k 0+0io 0pf+0w
  67. %
  68.  
  69. which isn't bad, but is much worse than it has to be.
  70.  
  71. In summary, there's much more to things than just finding a solution
  72. that works.  Try to find one that works well before you post it.
  73.  
  74. --
  75. Leif Jensen
  76. lhjensen@phoenix.princeton.edu
  77.