Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fastest way to display a certain line in a text file

Status
Not open for further replies.

eholdo

Technical User
May 30, 2002
31
US
I've writing a script that searches specific lines within a file and outputs them to a variable. Head | Tail combinations and sed -n ?p combinations all seem to be very slow as you get into a large file (25MB or so). Is there some way, maybe with awk or nawk to quickly send a specific line to standard output?

I'm writing this in csh.


Thanks!
 
a Perl script for you, should work on any perl v5 or greater.

$line_num = shift; # get line number from command line

while(<>){
next unless $. == $line_num;
last;
}

Script should be called with 2 arguments:

Line number to display and file to read. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Hi,
If the lines are fixed length, dd would be very fast. For example if you wanted the 10th line of a file with 80 character lines (including line feed),
dd if=infile of=outfile ibs=80 skip=9 count=1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top