LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-07-2011, 03:16 AM   #1
njfhdsiue
LQ Newbie
 
Registered: Jul 2011
Posts: 3

Rep: Reputation: Disabled
delete top lines of a text file until a word is met


Hi,

I need to chop of the top 30ish lines of several log files until a line starting with "Initialization completed."

The trouble is that it's not always the same amount of lines that need to be deleted, and they don't always contain the same information, which is why I would need to delete everything prior to the line starting with "Initialization completed."

Right now I have a little script I wrote based on looping each file through several "grep -v" commands with each known pattern of lines I want to ignore, but it is tedious and I have to inspect each file afterwards to make sure nothing is left from above "Initialization completed."

Any tips or ideas would be appreciated.

Thanks!
 
Old 07-07-2011, 03:23 AM   #2
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,

Is this what you are looking for:
Code:
sed '1,/^Initialization completed/d' infile
Hope this helps.

Last edited by druuna; 07-07-2011 at 03:25 AM. Reason: Fixed typo
 
1 members found this post helpful.
Old 07-07-2011, 03:27 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 again,

Not sure if you want to delete the Initialization completed line or not. If not:
Code:
sed '1,/^Initialization completed/c Initialization completed' infile
 
Old 07-07-2011, 03:48 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Just on top what druuna said:

If you want to make permanent changes to the file, you can either:
a) use the -i flag (the existing content will be overwritten):
Code:
sed -i '1,/^Initialization completed/c Initialization completed' infile
or
Code:
sed -i.bk '1,/^Initialization completed/c Initialization completed' infile
In this case a backup file (infile.bk) will be created with the original content.

b) redirect the output to a new file
Code:
sed '1,/^Initialization completed/c Initialization completed' infile > newfile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 08:55 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
Delete a set of lines after finding a text kbmukesh Linux - Newbie 3 06-16-2009 08:23 AM
how to delete two adjecent lines in a text file wangxinmco Linux - Newbie 3 01-10-2008 02:23 PM
How to Delete nnn lines from the top of a file with out opening it? rhelpm Linux - Newbie 6 12-07-2006 02:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:36 AM.

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