Removing lines from text file based on line number

Removing lines from text file based on line number

Post by Barry Phetteplac » Fri, 31 Jul 1998 04:00:00



Hello,

I am using ksh on HPUX 10.2 and I need help on the following:
If I have a text file 5000 - 6000 lines long and I grep for a string using
the -l option to print the line number that the string occurs on, is there
a way, through ksh to print out the lines starting from the position
returned from grep to the end of the file? So if grep finds the string on
line 1500 of 3000 lines, I want to print lines 1500 - 3000. This is just an
example, chances are I won't be printing 1500 lines to the screen. I've
tried using "tail -n <line returned by grep> textfile" but I've had mixed
results when working with large files. This will be a temp file I'm working
with so removing lines 1 - 1499 is fine too. Any ideas?

Thanks in advance.
Barry

 
 
 

Removing lines from text file based on line number

Post by Kurt J. Lanz » Fri, 31 Jul 1998 04:00:00



> Hello,

> I am using ksh on HPUX 10.2 and I need help on the following:
> If I have a text file 5000 - 6000 lines long and I grep for a string using
> the -l option to print the line number that the string occurs on, is there
> a way, through ksh to print out the lines starting from the position
> returned from grep to the end of the file? So if grep finds the string on
> line 1500 of 3000 lines, I want to print lines 1500 - 3000. This is just an
> example, chances are I won't be printing 1500 lines to the screen. I've
> tried using "tail -n <line returned by grep> textfile" but I've had mixed
> results when working with large files. This will be a temp file I'm working
> with so removing lines 1 - 1499 is fine too. Any ideas?

Don't use grep, use sed with the same RE:

        sed -n '/RE/,$p' yourfile

"man sed" for more info.

 
 
 

Removing lines from text file based on line number

Post by J. S. Jense » Fri, 31 Jul 1998 04:00:00



> If I have a text file 5000 - 6000 lines long...
> So if grep finds the string on line 1500 of 3000 lines, I want to print lines
> 1500 - 3000.
> I've tried using "tail -n <line returned by grep> textfile" but I've had
> mixed results when working with large files.

Using tail is exactly what you should use.  Of what ``mixed results'' do you
speak?

First off, what shell are you using?  You could always use sed with shell
aritmetic expressions:

sed -e "1,$[$(grep -n foo bar |cut -f 1 -d ':' |head -1) -1]d" bar >outputfile

or in bourne (without arithmetic expressions):

sed -e "1,$( echo $(grep -n foo bar |cut -f 1 -d ':' |head -1) -1 |bc)d" bar

Quote:>outputfile
> with so removing lines 1 - 1499 is fine too. Any ideas?

look at the above :-)

--
J. S. Jensen

http://www.paramin.com

 
 
 

Removing lines from text file based on line number

Post by Charles Dem » Sat, 01 Aug 1998 04:00:00




>Hello,

>I am using ksh on HPUX 10.2 and I need help on the following:
>If I have a text file 5000 - 6000 lines long and I grep for a string using
>the -l option to print the line number that the string occurs on, is there
>a way, through ksh to print out the lines starting from the position
>returned from grep to the end of the file? So if grep finds the string on
>line 1500 of 3000 lines, I want to print lines 1500 - 3000.

To get lines 1500-3000 of a file use one of these:

head -3000 infile | tail +1500
sed -n "1500,3000 p" infile

To get from line 1500 to the end, use one of these:

tail +1500 infile
sed -n "1500,$p" infile
sed    "1,1499d" infile

If you want to get from the first line containing a regular expression
to the end of the file:

sed -n "/regexp/,$p" infile
awk '/regexp/||c==1{c=1;print}' infile

see
man sed
man head
man tail
man awk
man gawk

or you can*around the way you're going, something like:

sed -n "`grep -n 'regexp' infile | cut -f1 -d: `,$ p" infile

or

tail  +`grep -n 'regexp' infile | cut -f1 -d: ` infile

Yuck!!!  :-)

FWIW, my grep -l doesn't do what you want.  It's grep -n on my system.

Chuck Demas
Needham, Mass.

Quote:>This is just an
>example, chances are I won't be printing 1500 lines to the screen. I've
>tried using "tail -n <line returned by grep> textfile" but I've had mixed
>results when working with large files. This will be a temp file I'm working
>with so removing lines 1 - 1499 is fine too. Any ideas?

>Thanks in advance.
>Barry

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

Removing lines from text file based on line number

Post by Tapani Tarvaine » Sat, 01 Aug 1998 04:00:00



> If I have a text file 5000 - 6000 lines long and I grep for a string using
> the -l option to print the line number that the string occurs on, is there
> a way, through ksh to print out the lines starting from the position
> returned from grep to the end of the file? So if grep finds the string on
> line 1500 of 3000 lines, I want to print lines 1500 - 3000.

sed -n 1500,3000p textfile

will print lines 1500-3000;

sed -n '1500,$p' textfile

will print from line 1500 to the end.

You might also do away with the grep:

sed -n '/string/,$p' textfile

will print lines from the first occurrence of "string" to
the end of the file.

--
Tapani Tarvainen

 
 
 

Removing lines from text file based on line number

Post by Ralf Draege » Sat, 01 Aug 1998 04:00:00



> Hello,

> I am using ksh on HPUX 10.2 and I need help on the following:
> If I have a text file 5000 - 6000 lines long and I grep for a string using
> the -l option to print the line number that the string occurs on, is there
> a way, through ksh to print out the lines starting from the position
> returned from grep to the end of the file? So if grep finds the string on
> line 1500 of 3000 lines, I want to print lines 1500 - 3000. This is just an
> example, chances are I won't be printing 1500 lines to the screen. I've
> tried using "tail -n <line returned by grep> textfile" but I've had mixed
> results when working with large files. This will be a temp file I'm working

Mixed results may come from the fact, that varios implementations of
tail
(like the one from SCO) work with a limited buffer. See the LIMITATIONS
section of your tail man-page:
---<snip>----
  Tails relative to the end of the file are kept in a buffer, and thus
are
  limited to 20480 characters or approximately 256 lines. Unpredictable
  results can occur if character special files are ``tailed''.
---<snip>----
Don't know, how this is implemented in HPUX.

cu, Ralf.
--

- Intraplan Consult Gmbh  Orleansplatz 5a  81667 Muenchen  +49 89
45911-0 -

Quote:>And, given that a a lot of people these days come to Unix from DOS (instead
>of the other way around - as God intended), [...]

Your God is a cruel god . . . ;-)    
                         (Jeremy Mathers & Jeffrey Drumm in
comp.unix.shell)