ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I know line number in a file which I want to output. Is there a command to accomplish this? For example:
> cmd -n 20 my_file
Would display only line number 20 in 'my_file'
You can use sed in any shell. It's just another program, like head and tail.
I think his point was that you could do this in other languages (perl, python, etc.), not just shells:
Code:
#!/usr/bin/perl -w
use strict;
my $line;
open (MYFILE, "/path/to/my_file") or die "Can't open data file: $!\n";
my $count = 0;
while ($line = <MYFILE>) {
$count++;
if ( $count == 20 ) {
print $line;
last;
}
}
close (MYFILE);
Much as I like perl, for one line, sed has this shot. It'll read the entire file, but that generally isn't an issue for a (semi-)reasonable sized file.
The ability to "short circuit" is handy for *really* big files - perl is useful here. I recently did some testing on printing blocks of lines from a file of 100 million lines. Short circuiting with Perl won - big time. Of course that was in scalar context.
I know line number in a file which I want to output. Is there a command to accomplish this? For example:
> cmd -n 20 my_file
Would display only line number 20 in 'my_file'
Much as I like perl, for one line, sed has this shot. It'll read the entire file, but that generally isn't an issue for a (semi-)reasonable sized file.
The ability to "short circuit" is handy for *really* big files - perl is useful here. I recently did some testing on printing blocks of lines from a file of 100 million lines. Short circuiting with Perl won - big time. Of course that was in scalar context.
By "short-circuit", are you referring to the '@array=<FILE>;' idiom? Most day-to-day work is well within that size. If I have files larger than that, I hope I know about it before processing, and I take the large size into account.
I found another method of printing using 'sed' which is good especially for big files since it stops when it reaches desired line number without reading the whole file.
sed '20q;d' my_file
I don't quite understand why it works though: 20q - tells to quit after first 20 lines; d - delete these lines. But it works. If somebody knows 'sed' good enough to explain, then please.
I would use "sed -n '21q;20p' file". For GNU sed, you can have two sed commands separated with a semicolon. Otherwise the command could be "sed -n -e '21q' -e '20p'"
This isn't hard to understand. The -n suppresses the output of every line read. The '21q' tells it to quit if it has read line 21. The '20p' says to print the line if it is line 20.
Cute - thanks jschiwal. This; "sed -n '20p;20q' file" also works, and for me is more (human) "readable".
As does this: "sed -n '20,23p;23q' file".
So now I can toss my perl script - thanks jschiwal ....
This is what I meant by "short circuit" (instead of needlessly continuing to read the entire file).
Just for entertainment I decided to run some tests on a file (5.7 million+ text records) on my laptop. Reboot before every run to make sure no cache issues.
Test 1: print records 100000-100005
1.390 secs - my perl (quits after the print; i.e. short circuits)
1 minute 14.323 secs - sed (without quit)
1.516 secs - sed with quit.
Test 2: print records 5700000-5700005
50.752 secs - my perl (quits after the print; i.e. short circuits)
1 minute 3.887 secs - sed with quit.
For me makes it worthwhile using the "short circuit" option on decent sized files.
And yes, I was surprised the perl ran faster - for that difference, you would just use sed. Not an exhaustive test, but interesting.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.