LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   printing only desired number of lines (https://www.linuxquestions.org/questions/programming-9/printing-only-desired-number-of-lines-784537/)

hammy123 01-25-2010 02:20 AM

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

carbonfiber 01-25-2010 02:23 AM

Code:

man tail

druuna 01-25-2010 03:32 AM

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.

H_TeXMeX_H 01-25-2010 08:57 AM

Code:

tail -n 50 file


All times are GMT -5. The time now is 02:34 PM.