Visit Our UNIX and Linux User Community


Grep and display multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and display multiple lines
# 1  
Old 04-13-2016
Grep and display multiple lines

Hi guys,

I have a log file that generates multiple logs about a query.

Code:
<query time='2016-04-13 13:01:50.825'>
  <PagingRequestHandler>
    <Before>brand:vmu</Before>
    <After>brand:vmu</After>
  </PagingRequestHandler>
  <GroupDeviceFilterHandler>
    <Before>brand:vmu</Before>
    <After>(brand:vmu) AND ( (type:shipping))</After>
  </GroupDeviceFilterHandler>
  <MultilangHandler>
    <Before>(brand:vmu) AND ( (type:shipping))</Before>
    <After>(brand:vmu) AND ( (type:shipping))</After>
  </MultilangHandler>
  <NumberFieldQueryRequestHandler>
    <Before>(brand:vmu) AND ( (type:shipping))</Before>
    <After>(brand:vmu) AND ( (type:shipping))</After>
  </NumberFieldQueryRequestHandler>
  <OnlyAviableAssetsRequestHandler>
    <Before>(brand:vmu) AND ( (type:shipping))</Before>
    <After>((brand:vmu) AND ( (type:shipping))) AND startTime:[0 TO 20160413180150825] AND (validUntil:notset OR validUntil:[20160413180150825 TO A])</After>
  </OnlyAviableAssetsRequestHandler>
  <RequestValidator>
    <Before>((brand:vmu) AND ( (type:shipping))) AND startTime:[0 TO 20160413180150825] AND (validUntil:notset OR validUntil:[20160413180150825 TO A])</Before>
    <After>((brand:vmu) AND ( (type:shipping))) AND startTime:[0 TO 20160413180150825] AND (validUntil:notset OR validUntil:[20160413180150825 TO A])</After>
  </RequestValidator>
  <QueryRequest langCode='en' pageSize='10' startIndex='1'>((brand:vmu) AND ( (type:shipping))) AND startTime:[0 TO 20160413180150825] AND (validUntil:notset OR validUntil:[20160413180150825 TO A])</QueryRequest>
  <QueryResult time='2016-04-13 13:01:50.889' totalRows='2' lastRow='2'/>
</query>

What I am interested in are 2 lines:

Code:
<query time='2016-04-13 13:01:50.825'>
<QueryResult time='2016-04-13 13:01:50.889' totalRows='2' lastRow='2'/>

I want to print these two lines for each block of <query>

I am trying something like this:

Code:
grep "query time" trace.log ; grep -A30 "query time" contentx-indexer-trace.log | grep "QueryResult time"

But all I can see is :

Code:
  <QueryResult time='2016-04-13 13:12:06.134' totalRows='27' lastRow='20'/>
  <QueryResult time='2016-04-13 13:12:06.223' totalRows='27' lastRow='27'/>
  <QueryResult time='2016-04-13 13:12:06.638' totalRows='52' lastRow='20'/>
  <QueryResult time='2016-04-13 13:12:06.668' totalRows='52' lastRow='40'/>
  <QueryResult time='2016-04-13 13:12:06.698' totalRows='52' lastRow='52'/>
  <QueryResult time='2016-04-13 13:12:09.292' totalRows='1' lastRow='1'/>

Any idea how can I get the query time and then its corresponding QueryResult time ?
# 2  
Old 04-13-2016
maybe something like:
Code:
sed -n '/<query time=/p; /<QueryResult time=/p;' logfile

# 3  
Old 04-13-2016
Try "alternation":
Code:
grep "<query time\|<QueryResult" file

# 4  
Old 04-13-2016
Do you mean:
Code:
grep -E '<query time|<QueryResult' file

?


--
Note: The use of \| with grep Basic Regular Expressions (BRE) is a GNU extension

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines for a particular year in a file using grep

hi, I have a log file with data for more than 3 years, i want only the rows for the year 2017, say for example. My file has the data like this 08-OCT-2015 11:17:35 AAA, BBBB 08-OCT-2017 11:17:35 AAA,Bdfdfd,dfdfd,dfd 08-Nov-2017 11:17:35 AAA,Bdfdfd,dfdfd,deree i want the rows... (2 Replies)
Discussion started by: skoshekay
2 Replies

2. Shell Programming and Scripting

Grep pattern and display all lines below

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)
Discussion started by: venkidhadha
5 Replies

3. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

4. UNIX Desktop Questions & Answers

Display a specific words from a multiple lines

well, i am so not familiar with this kind of things but i am gonna explain extactly what i am looking for so hopfully someone can figure it out :) i have a command that shows memory usage besides the process name, for example(the command output): 500 kb process_1 600 kb process_2 700 kb... (4 Replies)
Discussion started by: Portabello
4 Replies

5. UNIX for Dummies Questions & Answers

Using grep and zgrep then display the next few lines

Hello everyone. I would like to know if I can use grep or zgrep to search for a particular pattern then print the x number of lines after the pattern was found. Lets say for example a pattern was found on line 3, I wanted the output to show lines 3, 4 and 5. Thanks! (10 Replies)
Discussion started by: khestoi
10 Replies

6. Shell Programming and Scripting

Cut display multiple lines of output

I have a script that runs a command, and the output is on multiple lines. The fields are delimited by '='. I need the 8th column from the first line and the 2nd from the second line, but I can't figure out how to display the second line. command1 | cut -d '=' -f 8 The above gets me what I... (2 Replies)
Discussion started by: cbo0485
2 Replies

7. Shell Programming and Scripting

grep and display lines from a file

I have to grep on a few words in a file and then display the line containing those words and the line above it. For ex - File1.txt contains... abc xyz abc This is a test Test successful abc xyz abc Just a test Test successful I find the words 'Test successful' in the file... (6 Replies)
Discussion started by: user7617
6 Replies

8. Solaris

grep and display few lines before and after

Hi is there a way in grep to display few lines before and after the pattern?? I tried options A and B and after-context and before-context. But they don't work on Solaris platform. please advise. (13 Replies)
Discussion started by: melanie_pfefer
13 Replies

9. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

10. UNIX for Dummies Questions & Answers

Display multiple output lines

All, I have a file ABC.TXT which has two records: 12345 19.93 34.94 12345 94.84 10.48 If do the following command and grep '12345' ABC.TXT >> test1.txt If I look at the output of test1.txt I appears as follows: 12345 19.93 34.94 12345 94.84 10.48 I... (5 Replies)
Discussion started by: kingofprussia
5 Replies

Featured Tech Videos