Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

grep with exclude patter in shell

text file has below lines named ‘log’

--- [n-event-group-3] action
--- [n-event-group-8] action.service
--- [n-event-group-2] essing.service.
--- [n-event-group-10] service.
--- [ost-stats-event] host.
--- 1077916, partition: 10, 
--- [d | vm-cm-event] cm.service.
--- [d | vm-cm-eve] cm.service
error: not found
com.rom.nom
--- [-event-group-11] ervice
--- [-event-group-15] ervice
--- [-event-group-20] ervice
--- [-event-group-99] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice

want to grep lines containing [-event-group-13] and all line which does not have pattern [-event-group-(digits)]

so result needed is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

--- [ost-stats-event] host.
--- 1077916, partition: 10, 
--- [d | vm-cm-event] cm.service.
--- [d | vm-cm-eve] cm.service
error: not found
com.rom.nom
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice

I tried grep -vE '-event-group-[^13]' log but returns lines with

[n-event-group-3]
[n-event-group-10]
[n-event-group-11]
[n-event-group-15]

as well.

>Solution :

awk better fits your needs here:

$ awk '!/-event-group-/{print} /-event-group-13/{print}' file

--- [ost-stats-event] host.
--- 1077916, partition: 10, 
--- [d | vm-cm-event] cm.service.
--- [d | vm-cm-eve] cm.service
error: not found
com.rom.nom
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice
--- [-event-group-13] ervice
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading