LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   delete top lines of a text file until a word is met (https://www.linuxquestions.org/questions/linux-software-2/delete-top-lines-of-a-text-file-until-a-word-is-met-890382/)

njfhdsiue 07-07-2011 03:16 AM

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!

druuna 07-07-2011 03:23 AM

Hi,

Is this what you are looking for:
Code:

sed '1,/^Initialization completed/d' infile
Hope this helps.

druuna 07-07-2011 03:27 AM

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

sycamorex 07-07-2011 03:48 AM

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


All times are GMT -5. The time now is 09:03 PM.