LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 11-11-2017, 06:12 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Question 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
 
Old 11-11-2017, 06:24 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Code:
man grep
Hint, look for "Context Line Control"
 
1 members found this post helpful.
Old 11-11-2017, 06:24 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Of course it's achievable. As a search would tell you.
"man grep".
 
1 members found this post helpful.
Old 11-11-2017, 06:45 AM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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 ?
 
Old 11-11-2017, 06:48 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Quote:
Originally Posted by sysmicuser View Post
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.
Old 11-12-2017, 02:19 AM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TenTenths View Post
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..
 
Old 11-12-2017, 02:49 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
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.
Old 11-12-2017, 04:33 AM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
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
 
Old 11-13-2017, 04:44 PM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Any update from any learned guru ?
 
Old 11-13-2017, 05:50 PM   #10
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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
Code:
$ man grep
or a grep tutorial online, because you don't have a grasp on the basic usage.
 
Old 11-14-2017, 01:55 AM   #11
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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..
 
Old 11-14-2017, 05:08 AM   #12
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Quote:
Originally Posted by Turbocapitalist View Post
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
 
Old 11-14-2017, 05:21 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by TenTenths View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH snippet to insert a line AFTER a string match and comment line... arfon Programming 4 12-08-2015 12:55 PM
Delete line if match a string at specific position lwsunny Programming 2 12-12-2014 05:11 AM
Exact string match in multi line string Arun Shankar Programming 2 06-19-2014 09:54 AM
How to use Vi to match a string of text, add a new line, insert string... Slycraft Linux - Newbie 7 07-20-2012 10:24 AM
[SOLVED] [bash - sed - awk] Match line with x characters and add string TigerClaw Linux - Newbie 4 02-28-2012 12:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration