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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To read the last 500 lines

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have a large dataset and I cannot open the file, so I just want to read the last 500 lines of the dataset. I am sure there is a simple command in awk that will do so. Could any one please help me in this regard.
Thanks.
 
What about this ?
tail -500 /path/to/dataset

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Running the command generates the same file size.
 
And this ?
tail -500l /path/to/dataset
Anayway: man tail

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If tail won't work, awk can do it:
Code:
BEGIN {
  tail = 500
  if ( ARGC != 2 )
    exit
  ARGV[ 2 ] = ARGV[ 1 ]
  ARGC++
}

NR!=FNR && FNR > (NR-FNR-tail)
 
Hi futurelet,

Thanks for the code. It works fine. If I want to use the same code to extract first 500 lines, instead of TAIL what command should I replace with?
Thanks.
 
extract first 500 lines
awk 'NR<=500' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
[tt]
awk 'NR<=500' /path/to/input
[/tt]
Explanation. An awk program consists of optional function definitions and <test> { <action> } pairs. If the test yields true (i.e., non-zero), then the action is performed. Example:
[tt]
/minutes/ { total += $1 }
END { print "Total minutes:", total }
[/tt]
In the first line, a search is made in the line just read for the string "minutes"; if it's found, the result is TRUE and the action "total += $1" is performed (the number in field 1 is added to "total"). In the second line, the special test "END" is true after all of the file has been read.

In PHV's program, the action is missing, so awk assumes that the desired action is "{ print $0 }", i.e., print the line just read. "NR<=500" means "Is the number of the record (or line) just read less than or equal to 500?"

In the program I posted, it is necessary to find out the number of lines in the file before it would be possible to print the last 500 lines. So in the "BEGIN" action section, I take the filename that awk is planning to read and append it to ARGV, so that the filename appears twice and the file will be read twice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top