Monday, November 10, 2008

Extract a line from a file

There are many times I need to extract a single line (or possibly group of lines) from a file (Usually when things have gone wrong).

The easiest way to do this is as follows:

awk 'NR==lineNumber{print;exit;} filename

Replacing:

  lineNumber - with the line you wish display

  filename - with the name of your file





I have also seen the following suggested:

sed 'lineNumberq;d' filename

Replacing:

  lineNumber - with the line you wish display

  filename - with the name of your file


After performance testing on a large file (~10,000,000+ lines) the awk command was found to be faster.

No comments: