Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

awk - How to exclude several lines around match with grep or similar tool?

I can find all non-matched lines of file with grep -v 'my_pattern' some_file. Also i can print few strings before/after/around match with -A, -B or -C options of grep. But i can't combine these two options to exclude lines with pattern and certain amount of lines near matched lines - grep shows entire file as result. For example, i have log with a lot of patterns like this:

25.02.2012 10:41:37 here goes memory state
25MiB free
16MiB allocated
max free block is 4MiB

I'd like to filter them. Of course, i can write custom perl/awk script, but is there more elegant way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the vim text editor:

:g/my_pattern/-2,//+2d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...