Execution problem with print out record that follow specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution problem with print out record that follow specific pattern
# 1  
Old 02-25-2014
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 :
Code:
a       comp92  2404242 2405172
b       comp92  2405303 2406323
b       comp92  2408786 2410278
a       comp92  2410271 2410337
a       comp87  1239833 1240418
b       comp87  1240496 1240829
b       comp87  1241046 1243683
a       comp87  1243841 1244840
a       comp87  1243841 1244840
b       comp87  1244968 1246522

Output file :
Code:
a       comp92  2404242 2405172
b       comp92  2405303 2406323
a       comp87  1239833 1240418
b       comp87  1240496 1240829
a       comp87  1243841 1244840
b       comp87  1244968 1246522

Thanks.
This User Gave Thanks to patrick87 For This Post:
# 2  
Old 02-25-2014
Hi guy,
Try this:
Code:
sed -n '/^a[[:blank:]]\{1,\}/{N;/\nb[[:blank:]]\{1,\}/p;D}'

cheers.
These 2 Users Gave Thanks to Lucas_0418 For This Post:
# 3  
Old 02-25-2014
Awk approach with fixed strings:
Code:
awk '$1=="b" && k=="a" {print p ORS $0}{p=$0; k=$1}' file


--
@lucas: nice sed. \{1,\} can be left out, no?


*edit* it could also be done like this.
Code:
sed 'N;/^a[[:blank:]].*\nb[[:blank:]]/!D' file


Last edited by Scrutinizer; 02-25-2014 at 02:41 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-25-2014
Hi Scrutinizer,
Thanks Scurtinizer, thanks for your advice.
\{1,\} is not nessesary, a [[:blank:]] is totally enough, the sed code is better after you edited it.
This User Gave Thanks to Lucas_0418 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

2. Shell Programming and Scripting

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

Input file: 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: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

3. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

4. 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

5. 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

6. 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

7. 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

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