The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how do you parse 1 line at a time of file1 ie. line(n) each line into new file web_developer UNIX for Advanced & Expert Users 4 06-28-2009 12:07 PM
shell script to read a line in gps receiver log file and append that line to new file gudivada213 Shell Programming and Scripting 3 06-24-2009 06:16 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 72
Cheapest way to get line X from file

So, I have a couple of scripts that go through lots and lots of data-- sometimes thousands of smaller files, sometimes a few very large ones. It's very common that I need a particular line, by line number X. There are lots of ways to do this:

Code:
awk 'NR == X {print ; exit}' file
sed -n 'Xp' file
head -n X file | tail -n 1
There are others, too... My question is, what's the most economical way you know to do this? In my primitive tests (involving a large file and the time command), head/tail seem to be the winner. What is surprising is how badly sed did. On a file with 4237905 lines, here are the results:

Code:
awk                   sed                     head/tail
real    0m1.258s      real    0m22.469s       real    0m1.011s
user    0m1.010s      user    0m22.170s       user    0m0.550s
sys     0m0.250s      sys     0m0.280s        sys     0m0.730s
I actually expected sed to win. Strange... Anyway, are there any other methods I should consider? Are there improvements to these methods? Thanks.
  #2 (permalink)  
Old 3 Weeks Ago
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
try these from sed1liners:
Code:
 # print line number 52
 sed -n '52p'                 # method 1
 sed '52!d'                   # method 2
 sed '52q;d'                  # method 3, efficient on large files
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0