Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-11-2017, 06:12 AM
|
#1
|
Member
Registered: Mar 2010
Posts: 458
Rep:
|
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ?
Hey Guys,
Not sure if this is possible but I have a requirement, imagine a log file with content below
...1000 + lines
aaaknfdnfnjwefjn jwhbfewhjfb
cbcjhbdkqwhjwfl jhfwhfwjhqwjrie
TVSFSSJ.csv
whugefiuwqehhfowijf
khcbkjsdbkqjwfwhfej uihfowefoiwehjf
jwfvjwebfiwefwef nwehfiweuhfiewuhfi
....1000 + lines
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.
Is this achievable ?
Please assist. Thanks
|
|
|
11-11-2017, 06:24 AM
|
#2
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,562
|
Hint, look for "Context Line Control"
|
|
1 members found this post helpful.
|
11-11-2017, 06:24 AM
|
#3
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,365
|
Of course it's achievable. As a search would tell you.
"man grep".
|
|
1 members found this post helpful.
|
11-11-2017, 06:45 AM
|
#4
|
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
|
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 ?
|
|
|
11-11-2017, 06:48 AM
|
#5
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,562
|
Quote:
Originally Posted by sysmicuser
Secondly, I believe -A and -B does not give me the pattern match line.
|
Did you actually bother to try it? Your belief is wrong.
|
|
1 members found this post helpful.
|
11-12-2017, 02:19 AM
|
#6
|
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
|
Quote:
Originally Posted by TenTenths
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 switch however then line AFTER match are not displayed..
|
|
|
11-12-2017, 02:49 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,721
|
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.
|
|
1 members found this post helpful.
|
11-12-2017, 04:33 AM
|
#8
|
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
|
Quote:
Originally Posted by Turbocapitalist
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]#
Thanks in advance
|
|
|
11-13-2017, 04:44 PM
|
#9
|
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
|
Any update from any learned guru ?
|
|
|
11-13-2017, 05:50 PM
|
#10
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
You're not doing it right.
Code:
# grep -m 1 -A 2 -B 2 "SampleTextFile.txt" /var/log/messages
is looking for the string SampleTextFile.txt in /var/log/messages. You want
Code:
$ grep -m 1 -A 1000 -B 1000 some_string some_file
It works fine if you don't specify a file in place of the string, unless you want to find the file name in the specified file. I direct you back to or a grep tutorial online, because you don't have a grasp on the basic usage.
|
|
|
11-14-2017, 01:55 AM
|
#11
|
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
|
I'm doing it right and I am indeed grepping for a file name in /var/log/messages. Suggest you try exactly what I am doing and advise me if I am wrong.
I fail to understand what am I doing wrong..
|
|
|
11-14-2017, 05:08 AM
|
#12
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,562
|
Quote:
Originally Posted by Turbocapitalist
The -m option does not interfere with either -A or -B.
|
Looks like it does when the matching pattern appears in the -B block.
For example:
Code:
$ cat test
1
2
3
A
4
B
4
C
4
5
6
7
8
9
10
What the OP is expecting:
Code:
$grep -m 1 -A 2 -B 2 4 test Search for the first occurrence of 4
3
A
4
B
4
What actually happens in grep 2.20 is what the OP is observing:
Code:
$ grep -m1 -A 2 -B 2 4 test
3
A
4
B
So it does appear that the -m option does indeed interfere with -A
|
|
|
11-14-2017, 05:21 AM
|
#13
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,721
|
Quote:
Originally Posted by TenTenths
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.
|
|
|
All times are GMT -5. The time now is 10:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|