Help with print out record if first and next line follow specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with print out record if first and next line follow specific pattern
# 1  
Old 01-30-2015
Help with print out record if first and next line follow specific pattern

Input file:
Code:
pattern1   100   250   US
pattern2   50   3050   UK
pattern3   100   250   US
pattern1   70   1050   UK
pattern1   170   450   Mal
pattern2   40   750   UK
.
.

Desired Output file:
Code:
pattern1   100   250   US
pattern2   50   3050   UK
pattern1   170   450   Mal
pattern2   40   750   UK
.
.

Do anybody know how to print out only those record that column 1 is shown as "pattern1" and column 1 of next line follow by "pattern2"?
Thanks for any advice.
# 2  
Old 01-30-2015
Hello cpp_beginner,

Could you please try following and let me know if this helps.
Code:
awk '{C++} /pattern1/{B=$0;D=C} /pattern2/{D++;if(D==C){print B ORS $0}}' Input_file

Output will be as follows.
Code:
pattern1   100   250   US
pattern2   50   3050   UK
pattern1   170   450   Mal
pattern2   40   750   UK

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-30-2015 at 12:58 AM.. Reason: Changed solution a bit according to more combinations of requirement
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-30-2015
You could also try something like:
Code:
awk '
$1 == "pattern1" {		Found1 = 1; Save1 = $0; next }
Found1 && $1 == "pattern2" {	print Save1 ORS $0 }
{				Found1 = 0 }
' file

which would seem to be closer to what you requested if pattern1 or pattern2 could appear anywhere on the line besides the 1st field.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-30-2015
Thanks, Cragun.
It worked perfect for my case Smilie

---------- Post updated at 12:57 AM ---------- Previous update was at 12:56 AM ----------

Thanks, Singh.
It worked fine for my case as well Smilie
Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

2. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

3. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

4. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

5. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

6. Shell Programming and Scripting

Help with print out all relevant record if match particular pattern

Input file: data100_content1 420 700 data101_content1 107 516 data101_content2 194 773 data101_content3 195 917 data104_content2 36 325 data105_content1 505 605 data106_content1 291 565 ... (7 Replies)
Discussion started by: perl_beginner
7 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Programming

Print specific pattern line in c++

Input file: @HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC +HWI-BRUNOP1_header_1 TNTTJTTTETceJSP__VRJea`_NfcefbWe Desired output file: >HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC >HWI-BRUNOP1_header_2... (10 Replies)
Discussion started by: cpp_beginner
10 Replies

9. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

10. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies
Login or Register to Ask a Question