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

After running the following command, I get:

[root@yukiko /]# find / -iname .bashrc

But I would like to have a command that prints a specific line by supplying the command with the line number, for example:

[root@yukiko /]# find / -iname .bashrc | getline(2)

Is there such a command on CentOS?

share|improve this question
up vote 4 down vote accepted

You could use a combination of head and tail:

find / -iname .bashrc | head -n 2 | tail -n 1

this will return the last one row of the first two rows (so the second row). You might be able to get head to do the job on its own if it has an option for skipping X number of rows (check man head). It is also not unlikely that there is another tool to do the job in the set that is the coretulis package included by most Linux variants so it migth be worth scanning the manual for this group of tools (see or your local copy) a quick scan. It is worth a look anyway if only to refresh yourself on what tools are commonly available.

share|improve this answer
find / -name .bashrc | sed -n '2{p;q}'
share|improve this answer
+1 for our good friend sed. With this the OP has the foundation for just what he wants, e.g.: getline () { LN=$1; shift; sed -n "${LN}{p;q}" "$@"; } – pilcrow Feb 8 '10 at 22:04
And a +1 for your comment; that's a nice way of hiding sed syntax from those unfamiliar with it. – njd Feb 10 '10 at 17:47

Here is a whip-up of a Perl script to do that

use strict; 
use warnings;

my $ln = $ARGV[0];
#note that further evolution may have a regular file here...
my $line = 1; 
my $wantedline;
  $wantedline = $_;
  last if($line++ == $ln);
print $wantedline;
share|improve this answer
Now that's a lot longer than a typical Perl one-liner... perl -ne 'if($.==2){print;last}' – grawity Feb 10 '10 at 19:38
@grawity: That's OK, right? – Paul Nathan Feb 11 '10 at 2:32
It's OK because it is more readable and uses a command line argument instead of hardcoding the number, but it's also not OK because 1) $wantedline is completely unneeded and 2) Perl already has a special var $. for current line number. – grawity Feb 11 '10 at 13:51
@grawity: Readability trumps those concerns unless you're skilled in Perl. At least IMO. – Paul Nathan Feb 11 '10 at 22:50

You must log in to answer this question.

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