Grep three consecutive lines if each lines contains certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep three consecutive lines if each lines contains certain string
# 1  
Old 11-17-2015
Linux Grep three consecutive lines if each lines contains certain string

say we have :

Code:
2914 | REQUEST | whatever
2914 | RESPONSE | whatever
2914 | SUCCESS | whatever
2985 | RESPONSE | whatever
2986 | REQUEST | whatever
2990 | REQUEST | whatever
2985 | RESPONSE | whatever
2996 | REQUEST | whatever
2010 | SUCCESS | whatever
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever
2076 | REQUEST | whatever
2078 | RESPONSE | whatever

should print:
Code:
2914 | REQUEST | whatever
2914 | RESPONSE | whatever
2914 | SUCCESS | whatever
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever

already in one of the threads below command is used for 2 consecutive line. I want similar command for 3 consecutive line:
Code:
awk -F"|" '$2 ~ "REQUEST" {s=$0;f=1;next} f && $2 ~ "RESPONSE" {print s RS $0;f=0}' file

Also, please explain how the command works. I have very basic knowledge about awk.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags yourself. I have provided a few from my own wealth of them as a welcome give, but from now on i will tax every occasion where i have to spend some on your texts with infraction points. Use CODE-tags, as required by the forum rules and we will get along well, though.

Last edited by bakunin; 11-17-2015 at 11:04 AM..
# 2  
Old 11-17-2015
That is a very vague description. Please be way more detailed and specific. Why should we select 2914 | REQUEST and not 2986 | REQUEST? Your code snippet would print a "REQUEST" line and the very next "RESPONSE" line coming up, independent of contents.
# 3  
Old 11-17-2015
Sonu Pandy , right ? Smilie
# 4  
Old 11-17-2015
Something like this (untested)
Code:
awk -F"|" '
f==2 && $2~"SUCCESS" {print s RS $0; f=0}
f==1 && $2~"RESPONSE" {s=s RS $0; f=2}
$2~"REQUEST" {s=$0; f=1}
' file


Last edited by MadeInGermany; 11-19-2015 at 07:20 AM.. Reason: RS not ,
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-19-2015
Quote:
Originally Posted by RudiC
That is a very vague description. Please be way more detailed and specific. Why should we select 2914 | REQUEST and not 2986 | REQUEST? Your code snippet would print a "REQUEST" line and the very next "RESPONSE" line coming up, independent of contents.
I want to print a pattern with request, response and success in order. Whenever they are together it should print and skip all others.

like the below pattern irrespective of the number given in beginning:
Code:
2914 | REQUEST | whatever
2914 | RESPONSE | whatever
2914 | SUCCESS | whatever


Last edited by Scrutinizer; 11-20-2015 at 05:06 AM..
# 6  
Old 11-19-2015
Hello Saumitra,

If understood your requirement correctly then following may help you in same where you need to have columns values in sequence of REQUEST RESPONSE SUCCESS and column 1 should have all 3 then only it should print it. If you have some other requirement please let us know with sample input and all conditions with sample expected output.
Code:
awk -F' +| +' 'FNR==NR{A[$1 OFS $3]=$0;next} (($1 OFS "REQUEST") in A){O=A[$1 OFS "REQUEST"];B[$1]++} (($1 OFS "RESPONSE") in A){O=O ORS A[$1 OFS "RESPONSE"];;B[$1]++} (($1 OFS "SUCCESS") in A){O=O ORS A[$1 OFS "SUCCESS"];;B[$1]++}{if(B[$1]==3 && O !~ /^$/){print O;O="";}}'  Input_file Input_file

Output will be as follows.
Code:
2914 | REQUEST | whatever
2914 | RESPONSE | whatever
2914 | SUCCESS | whatever
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever

EDIT: Adding a non-one liner form for solution here too.
Code:
awk -F' +| +' 'FNR==NR{
                        A[$1 OFS $3]=$0;
                        next
                      }
               (($1 OFS "REQUEST") in A)        {
                                                        O=A[$1 OFS "REQUEST"];
                                                        B[$1]++
                                                }
               (($1 OFS "RESPONSE") in A)       {
                                                        O=O ORS A[$1 OFS "RESPONSE"];
                                                        B[$1]++
                                                }
               (($1 OFS "SUCCESS") in A)        {
                                                        O=O ORS A[$1 OFS "SUCCESS"];
                                                        B[$1]++
                                                }
                                                {
                                                        if(B[$1]==3 && O !~ /^$/){
                                                                                        print O;
                                                                                        O="";
                                                                                        i=""
                                                                                 }
                                                }
               ' Input_file  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-19-2015 at 08:23 AM.. Reason: Added a non-one liner form of solution now.
# 7  
Old 11-19-2015
Quote:
Originally Posted by MadeInGermany
Something like this (untested)
Code:
awk -F"|" '
f==2 && $2~"SUCCESS" {print s RS $0; f=0}
f==1 && $2~"RESPONSE" {s=s RS $0; f=2}
$2~"REQUEST" {s=$0; f=1}
' file

thanks buddy. this worked.. Smilie

---------- Post updated at 07:17 PM ---------- Previous update was at 06:03 PM ----------

Hey Ravindra,

Thanks for your solution. The code looked a bit complicated to me. It's working fine but it's skipping some lines which it should not:

Code:
[abc]$ cat text
2985 | RESPONSE | whatever
2990 | REQUEST | whatever
2985 | RESPONSE | whatever
2996 | REQUEST | whatever
2010 | SUCCESS | whatever
asdgahna
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever
afbb
2076 | REQUEST | whatever
2078 | RESPONSE | whatever
2452 | SUCCESS
[abc]$ ./script.sh
2013 | REQUEST | whatever
2013 | RESPONSE | whatever
2013 | SUCCESS | whatever

It didn't print :
Code:
2076 | REQUEST | whatever
2078 | RESPONSE | whatever
2452 | SUCCESS

I guess it's my mistake I didn't explain my query completely.
anyways thanks a lot.

Last edited by Scrutinizer; 11-20-2015 at 05:04 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate consecutive lines with specific string

Hello, I'm trying to remove the duplicate consecutive lines with specific string "WARNING". File.txt abc; WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 WARNING 2345 bcd; abc; 123 123 123 WARNING 1234 WARNING 2345 WARNING 2345 efgh; (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

3. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

4. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

5. Shell Programming and Scripting

aix :grep to get lines before and after string

am using AIX and I have a string "There is no process to read data written to a pipe". I want to get the output 2 lines before and 4 lines after this string. The string is present like more than 100 times in the log and I want to output, the last result in the log with this string I tried using... (1 Reply)
Discussion started by: PhAnT0M
1 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

8. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

9. Shell Programming and Scripting

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

10. Shell Programming and Scripting

grep string & next n lines

need help on this. let say i hv 1 file contains as below: STRING Description bla bla bla Description yada yada yada Data bla bla Data yada yada how do i want to display n lines after the string? thanks in advance! (8 Replies)
Discussion started by: ashterix
8 Replies
Login or Register to Ask a Question