Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Its about Unix and Linux.

Yes, there are many solutions to that, e.g. see http://mywiki.wooledge.org/BashFAQ/011. 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
add comment

5 Answers

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
add comment

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
add comment

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
add comment

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
1  
oh, i see this is the first example in the FAQ you mentioned ... –  user880480 Jan 5 '12 at 11:00
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.