LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2007, 08:04 PM   #1
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Display certain line in a file


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'

Thanks
 
Old 07-11-2007, 08:39 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What's "cmd"? (Not on my machine)

You would need to state what language you are using.

In bash, you can use sed for this
 
Old 07-11-2007, 08:41 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
There are at least a couple of ways to do that. To get the 20th line of my_file:
Code:
sed -n '20p' my_file
head -n 20 my_file | tail -n 1
I'm sure others have more...
EDIT: I'm assuming shell scripting
 
Old 07-11-2007, 08:47 PM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Quote:
Originally Posted by pixellany
In bash, you can use sed for this
You can use sed in any shell. It's just another program, like head and tail.
 
Old 07-11-2007, 10:37 PM   #5
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Original Poster
Rep: Reputation: 31
Both 'head' and 'sed' work fine in my Bash scripting file. Much more elegant way then I had before with awkward 'for' loop.

Thanks.
 
Old 07-11-2007, 11:58 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Quote:
Originally Posted by zhangmaike
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);
 
Old 07-13-2007, 04:06 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If the file is not huge, perl can do it as a one-liner:
Code:
perl -e '@file=<>;print $file[19];' yourFileName.ext
I assumed that your line numbers start at 1.
--- rod.
 
Old 07-13-2007, 04:49 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 07-13-2007, 05:19 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by rsashok
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'

Thanks
Code:
awk 'NR==20' file
 
Old 07-13-2007, 12:27 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by syg00
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.

--- rod.
 
Old 07-13-2007, 06:33 PM   #11
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Original Poster
Rep: Reputation: 31
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.
 
Old 07-13-2007, 06:44 PM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 07-13-2007, 09:36 PM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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).
 
Old 07-14-2007, 09:56 PM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Head prints the head of a file; tail reads the end of a file. We need a "tummy" command to read the middle of a file!
Code:
#!/bin/bash
if [[ ${#@} -ne 3 ]]; then
   cat <<-EOF
      usage: $0 filename startnumber endlinenumber
EOF
   exit 1
fi

sed -n "$2,$3p;$(($3+1))q" "$1"
 
Old 07-15-2007, 09:49 PM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Display line number n from a file doctorwebbox Linux - Newbie 2 01-05-2005 02:06 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration