Home » Questions » Computers [ Ask a new question ]

Getting the line of string from previous pipe output by line number

Getting the line of string from previous pipe output by line number

After running the following command, I get:

Asked by: Guest | Views: 248
Total answers/comments: 2
Guest [Entry]

"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 http://www.gnudotorg/software/coreutils/manual/html_node/index.html or your local copy) a quick scan. It is worth a look anyway if only to refresh yourself on what tools are commonly available."
Guest [Entry]

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

#!/usr/bin/perl
use strict;
use warnings;

my $ln = $ARGV[0];
#note that further evolution may have a regular file here...
my $line = 1;
my $wantedline;
while(<STDIN>)
{
$wantedline = $_;
last if($line++ == $ln);
}
print $wantedline;"