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.
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.
[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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.