How to capture 1000 lines before a string match and 1000 line a string match including line of string match ?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What I am interested is to extract 1000 lines before "TVSFSSJ.csv" pattern match line, line with "TVSFSSJ.csv" and 1000 lines after "TVSFSSJ.csv" pattern match line.
Thank you. I did man grep and answer is -A and -B but I still have two more questions. I want the line from the first time pattern match and 1000 lines before and after the first pattern match. Secondly, I believe -A and -B does not give me the pattern match line. How do we get that ?
Did you actually bother to try it? Your belief is wrong.
Yep, it gives me pattern match line BUT my requirement is yet not fulfilled. Whenever there is a pattern match I want grep to be 1000 lines before and after for only first occurrence when you read the file top to down. Unfortunately it is giving for all occurrences which doesn't help me. I tried with
Code:
-m 1
switch however then line AFTER match are not displayed..
The -m option does not interfere with either -A or -B.
So please post the actual options you are using as well as the version: grep -V.
Answer to first question.
Just grep without -m option
Code:
[root@centos6 bin]# grep -A 2 -B 2 "SampleTextFile.txt" /var/log/messages
Nov 10 00:14:54 centos6 kernel: Kernel logging (proc) stopped.
Nov 10 00:14:54 centos6 rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1513" x-info="http://www.rsyslog.com"] exiting on signal 15.
File SampleTextFile.txt
First Line after original SampleTextFile.txt
Nov 12 18:13:48 centos6 kernel: imklog 5.8.10, log source = /proc/kmsg started.
Nov 12 18:13:48 centos6 rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1519" x-info="http://www.rsyslog.com"] start
--
Nov 12 18:14:19 centos6 polkitd[2382]: started daemon version 0.96 using authority implementation `local' version `0.96'
Nov 12 18:14:19 centos6 rtkit-daemon[2388]: Sucessfully made thread 2386 of process 2386 (/usr/bin/pulseaudio) owned by '42' high priority at nice level -11.
File SampleTextFile.txt
Second Line after original SampleTextFile.txt
Nov 12 18:14:19 centos6 pulseaudio[2386]: alsa-util.c: Disabling timer-based scheduling because running inside a VM.
Nov 12 18:14:19 centos6 rtkit-daemon[2388]: Sucessfully made thread 2392 of process 2386 (/usr/bin/pulseaudio) owned by '42' RT at priority 5.
[root@centos6 bin]#
Now when I use -m option
Code:
[root@centos6 bin]# grep -m 1 -A 2 -B 2 "SampleTextFile.txt" /var/log/messages
Nov 10 00:14:54 centos6 kernel: Kernel logging (proc) stopped.
Nov 10 00:14:54 centos6 rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1513" x-info="http://www.rsyslog.com"] exiting on signal 15.
File SampleTextFile.txt
[root@centos6 bin]#
Lastly grep version 2.20
Code:
[root@centos6 bin]# grep -V
grep (GNU grep) 2.20
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
[root@centos6 bin]#
Looks like it does when the matching pattern appears in the -B block.
Ah. I had tested several permutations but not that one. I suspect there may be no work-around with grep. There may be a short way to do it in AWK or perl.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.