Is there a “canonical” way of doing that? I’ve been using head -n | tail -1 which does the trick, but I’ve been wondering if there’s a Bash tool that specifically extracts a line (or a range of lines) from a file.
By “canonical” I mean a program whose main function is doing that.
headand pipe withtailwill be slow for a huge file. I would suggestsedlike this:Where
NUMis the number of the line you want to print; so, for example,sed '10q;d' fileto print the 10th line offile.Explanation:
NUMqwill quit immediately when the line number isNUM.dwill delete the line instead of printing it; this is inhibited on the last line because theqcauses the rest of the script to be skipped when quitting.If you have
NUMin a variable, you will want to use double quotes instead of single: