LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-25-2010, 02:20 AM   #1
hammy123
LQ Newbie
 
Registered: Jan 2010
Posts: 1

Rep: Reputation: 0
printing only desired number of lines


hi

suppose there are 100 lines in my file.and i want to save (redirect) line nu-50 to 100 in onother file.
but plz note that line number 50 will be given by the user, so it can be anything.
suppose if user print 67, then i have to process user input & print 67-100 lines
i think u get my problem.

thanks

Last edited by hammy123; 01-25-2010 at 02:26 AM.
 
Old 01-25-2010, 02:23 AM   #2
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Code:
man tail
 
Old 01-25-2010, 03:32 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Here's a rough example using sed:
Code:
#!/bin/bash

STARTAT=$1

INFILE="infile"
OUTFILE="output"

sed -n ''$STARTAT',$p' $INFILE > $OUTFILE
Testrun:
Quote:
$ cat infile
1
2
3
4
5
6
7
8
9
10

$ ./show.selection 7
$ cat output
7
8
9
10
A range can be printed by sed as follows: sed -n 'x,yp' infile ($, as used in the example, is special, this tells sed to print up to and including the last line). To use shell variables inside a sed statement you need to use the correct quoting.

Hope this helps.
 
Old 01-25-2010, 08:57 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Code:
tail -n 50 file
 
  


Reply



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
number of Lines displayed in Terminal roadrash Linux - Newbie 1 07-03-2009 06:52 AM
number of lines to the new file coppuca Linux - Newbie 7 01-06-2009 10:17 AM
Viewing specified number of lines sir_com Linux - Newbie 2 11-17-2008 12:27 AM
number of lines for runlevel 3 RAFAL Linux - General 3 07-24-2008 11:53 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM

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

All times are GMT -5. The time now is 03:21 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