I need to print out a particular record, which is the 3d from the tail, that is NR == total NR-2. I’ve been trying to do that for almost an hour and still can’t get it work, so this is what I have:
cat myfile | grep mystring | awk 'END {NR == $(NR-2); print $0}'
This gives me the last NR, why?
awkprocesses each line and just throws it out when it’s done, you have to save it manually [N.B. YourNR == $(NR-2)checks whether the number of rows is equal to the (number of rows – 2)th field in the last row]. Easiest would be to store them all, but that’s not very memory efficient so we can store just the last three we’ve seen. Actually the easiest would be to useheadandtail:Or with
awk: