Delete lines starting with these strings

Platform : RHEL 5.8

I have text file called myapplication.log . In this file, I have around 800 lines which start with the followng three strings

PWRBRKER-3493
PWRBRKER-7834
SCHEDULER-ERROR

How can I delete these lines in one go ?

Have you read the manual pages for egrep?

Have a look at the -v option and consider the special character ^ meaning the start of the record.

Does this help you out?

Robin
Liverpool/Blackburn
UK

1 Like

Thank you Robin. My requirement is to delete those lines. So grep or egrep won't help.

This is what Robin meant:

egrep -v "^PWRBRKER-3493|^PWRBRKER-7834|^SCHEDULER-ERROR" myapplication.log > tmp; mv tmp myapplication.log
1 Like

Thank you very much Yoda.

awk '!/^PWRBRKER-3493|^PWRBRKER-7834|^SCHEDULER-ERROR/' myapplication.log

if you want to delete first 3 lines compulsorily, without matching the patterns then you can use

sed '1,3d' myapplication.log > myapp;mv myapp myapplication.log

deletes the 1st 3 lines from the file.

1 Like

Or with awk

awk 'NR>3' myapplication.log

@Little
This:
sed '1,3d' myapplication.log > myapp;mv myapp myapplication.log
can be replaced by this:
sed -i '1,3d' myapplication.log
-i deletes it directly in the file, no need print, move etc

1 Like

Dear Little and Jotne,

I don't think that this was the requirement, but thanks for the code. I will use those in other things where I've previously messed about with head & tail :o

Many thanks for smartening my code! It shows we can all learn. :slight_smile:

Robin

Not sure about that:

I have around 800 lines which start with the followng three strings
PWRBRKER-3493
PWRBRKER-7834
SCHEDULER-ERROR

Yes, I suppose it depends on interpretation. Mine was that there were 800 or so unwanted lines in the file and each of them started with the given strings, and needed to be removed to leave the rest. I can see how it can also be read that there is an 800 line file and the first three records need to be removed.

Oh well, I suppose that between us, we've answered both. :b:

omega3 can you confirm that you have an answer one-way or another?

Thanks,
Robin

thanks i didnot knew this option.

You should be careful when moving new files over old ones if the files permissions, metadata, inode number, etc. are important.

blah .... > newfile && cp newfile oldfile && rm newfile

sed -i does not do anything directly in the file. Just like using > and mv , it writes a new file which it then moves.

The most notable side-effect of using -i is that an otherwise portable script such as 1,3d is rendered unusable with most sed implementations. In such cases, -i is worse than worthless.

Regards,
Alister