Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Its about Unix and Linux.

Yes, there are many solutions to that, e.g. see Some of them are fast as hell (not reading the whole file) and some are slow but short and much more readable. I am interested in a solution that works on small files < 1MB, that is easy to remember and easy readable and should finish below 1sec. Of course I could write a script or shell alias, but when I come to new installed computers I have to reestablish my environment first, so I prefer a solution that works with most Unix and Linux Distris out of the box, or at least after installing the desired tool via package management. Can someone beat this?:

 awk NR==42 file
share|improve this question
up vote 1 down vote accepted

The awk and perl solutions both have the disadvantage that they continue reading after printing the Nth line. The yes command, which prints infinitely many lines, can be used to test this.

A short solution that shares this problem is:

 sed -n 42p file

This one quits after printing the line:

sed -n '42{p;q}' file
share|improve this answer
As said performance is not much very important to me. I think sed -n 42p file is the only rival I guess. – math Jan 5 '12 at 11:49
I think sed -n 42p file is better than awk NR==42 file since when using the awk snippet you must remember == and NR and also that NR must be in upper case. The sed snippet comes really close for an ideal command: cmd -n 42 file. You just have to remember the p for printing. – math Jan 6 '12 at 9:39

As long as there is a head and tail command

This will print the 12th line of file abc. Keep tail -n 1, and just change 12 to whatever line number you want to print.

$ head -n 12 abc | tail -n 1

If you're willing to install the a tool via package management, then you should be willing to use a script you write, and also, i'd guess that maybe all your linux and unix distros have head and tail commands. but if not then install them, or install awk or perl, of course whatever you use will work the same on any system, as long as the system has the tool, be it head and tail, awk or perl.

share|improve this answer
yes you could even improve: head -42 file | tail -1 but this is too long for me :) – math Jan 5 '12 at 12:06
@math well since you're willing to write a script, you can shorten any command to just $disp 42 <ENTER> and it could have this line or a sed line or whatever, for whatever your linux distros share.. head and tail is probably very common. – barlop Jan 6 '12 at 13:33

I don't think I can beat your AWK example for brevity or readability, but here is a Perl example

  perl -ne 'print if $. == 42' file
share|improve this answer
Nothing can beat Perl when it comes to portability among systems and architectures. Just keep your aliases and .bashrc in a central repository, git clone to new systems. – grawity Jan 5 '12 at 10:20

use sed:

sed -n 42p file

It is easy to type (no uppercase letters, no need to hit "Shitft") and is very short.

share|improve this answer
oh, i see this is the first example in the FAQ you mentioned ... – user880480 Jan 5 '12 at 11:00

You can use the sed. In the following command, the -n option disables printing each line, so only explicit printing (p commands) is carried out. 12 matches the 12th line and p issues a print.

sed -n '12 p'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .