Grep lines before a pattern having some other pattern
Hi All,
I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help.
Input: test.xml
results into
But the problem I am facing is that I don't know where "SESSION DESCRIPTION" will be present? will it be at the 15th line, 10th line above the "Thread" pattern? Also I've to capture the first "SESSION DESCRIPTION" pattern when multiple are present. So how to decide the NUM for -B or is there a way to capture the first pattern encountered before the line grep-ed?
If I understand what you're trying to do, I think you want something like:
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Note, however, that this will print all lines from a line containing SESSION DESCRIPTION up through the next line containing Thread even if there is no line containing Thread after a line containing SESSION DESCRIPTION. It isn't clear to me if you want that or not. If you only want sets of lines that contain both strings, the code is slightly more complicated.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
I could not tell if only the 2 lines matching 2 patterns were desired, or if the entire section between those two lines was wanted. So here is a solution for both, using easily accessible grep-like utilities:
exit 0
producing:
peg: http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.14
Both codes have many more features than noted here. Regarding peg, because it is in perl, it can be taken to almost any platform that has perl. The cgrep code is c, which means that it must be compiled. I had no trouble compiling it on 32-bit and 64-bit systems with gcc.
Linux version : Oracle Linux 6.5
Shell : bash
In the the below text file (someString.text), I want to grep all lines with .sh in it. ie. Only the lines mysript.sh and anotherscript.sh should be returned.
My below attempts failed.
I gather that in regular expression world, dot (.) is the... (3 Replies)
The intended result should be :
PDF converters
'empty line'
gpdftext and pdftotext?xml version="1.0"?>
xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters
gpdftext and pdftotext</note-content>... (9 Replies)
Hi all!
Thanks for taking the time to view this!
I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern.
Example:
Drink a soda
Eat a banana
Eat multiple bananas
Drink an apple juice
Eat an apple
Eat multiple apples
I... (8 Replies)
Hi all,
I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing.
I have used the below code
grep -A 3 "pattern" filename
But it is throwing error as below.
grep: illegal option -- A
Can... (14 Replies)
Hi I need to grep for a patter and display all lines below the pattern.
For ex: say my file has the below lines
file1
file2
file3
file4
file5
I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Hi ,
I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows.
# diff mir_lex.c.modified mir_lex.c.orig
3209c3209
< if(yy_current_buffer -> yy_is_our_buffer == 0) {
---
>... (5 Replies)
Hi,
I have got the below requirement. please suggest.
I have a file like,
Processing Item is:
/data/ing/cfg2/abc.txt
/data/ing/cfg3/bgc.txt
Processing Item is:
/data/cmd/for2/ght.txt
/data/kernal/config.klgt.txt
I want to process the above file to get the output file like,
... (5 Replies)
Hi all,
I am looking for a quick solution for this:
I have many log files of an iterative program, and I would like to display the parameters of the last three iteration from each of those files. Relevant lines have the keyword: ITER
I am using:
tac ~/modeling*/fitting.log | grep -m 3 -e... (2 Replies)
OK. I have a file I'd like to be able to grep, but on top of returning the line where the pattern matches, I'd like to be able to get the previous 8 lines and the following 8 lines. Is there a way to do this? (2 Replies)
Hi All,
OS-Type=Sun-OS 5.8 Sparc9 Processor
Can I grep the previous 4 lines and next 4 lines of a matched pattern(context grep)?
For example here we need to monitor logs of live traffic.The data obtained from "tail -f LiveTrafficData.log"
looks something like this:-... (3 Replies)