Operating System - Linux
1752806 Members
5893 Online
108789 Solutions
New Discussion юеВ

Strip bottom of text file based on pattern

 
SOLVED
Go to solution
Tom Weber_4
Advisor

Strip bottom of text file based on pattern

I'm trying to think of the best way (awk, sed, perl, etc.) to cut off the bottom of a text file when it encounters a certain pattern. Let's say a 100 line text file and if it sees a certain word or phrase, return only the lines above the pattern.

So if it finds the pattern in line 67, return the first 66 lines.

Thanks!
5 REPLIES 5
Wouter Jagers
Honored Contributor
Solution

Re: Strip bottom of text file based on pattern

How about:

sed '/the word/,$d' < input.txt

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
James R. Ferguson
Acclaimed Contributor

Re: Strip bottom of text file based on pattern

Hi Tom:

# perl -pe 'last if /pattern/' file

Regards!

...JRF...
Tom Weber_4
Advisor

Re: Strip bottom of text file based on pattern

That was quick! I knew someone out there would be all over it. Thank you!
Tom Weber_4
Advisor

Re: Strip bottom of text file based on pattern

Thanks Wout and James! Both solutions work just fine.
Tom Weber_4
Advisor

Re: Strip bottom of text file based on pattern

I received two replies which both are solutions to my question.