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