Given a file:
dept4.abc.edu
dept3.abc.edu
dept2.abc.edu
dept1.abc.edu
I know how to get the 3rd line from the bottom using the command:
tail -3 file | head -1
This is okay as long as the file length is greater or equal 3 lines [ $(wc -l < file) -gt 3 ]
. So tail -4 file | head -1
is still fine, but tail -5 file | head -1
is not what I really want.
I am wondering if there is a better way and cleaner way in getting the nth line safely, by saying safely I mean if it is not there, just return me an empty string or error. Any ideas?