The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If conditional abhinavsinha Shell Programming and Scripting 12 11-03-2008 06:30 AM
If conditional abhinavsinha UNIX for Dummies Questions & Answers 1 10-31-2008 01:48 AM
Conditional FTP Dastard Shell Programming and Scripting 2 06-21-2008 08:38 AM
conditional ftp dr46014 UNIX for Dummies Questions & Answers 3 06-20-2008 12:00 PM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 01:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
conditional extracting

Hi,

I need to extract lines based on some conditions as explained below:

File format details:

1. each set starts with AAA only
2. number of columns is fixed
3. number of rows per set may vary (as one set is upto DDD - 4 rows)

Now, i need to extract only the lines starting with AAA and BBB only. The condition is to pick AAA and BBB if and only if both exist in the same data set.

For. e.g, i have data for 4 sets and file has the below:

AAA,1,a,b,c,d
CCC,1,p,q,r,s
BBB,1,j,k,l,m
AAA,2,j,k,l,m
BBB,2,a,b,c,d
AAA,3,w,x,y,z
CCC,3,p,q,r,s
DDD,3,a,b,c,d
BBB,3,j,k,l,m
AAA,4,w,x,y,z
CCC,4,p,q,r,s

then the output must be

AAA,1,a,b,c,d
BBB,1,j,k,l,m
AAA,2,j,k,l,m
BBB,2,a,b,c,d
AAA,3,w,x,y,z
BBB,3,j,k,l,m

(Pl note there is no 4th data set in the output as 4th data set has no BBB)

Please advise.

Thanks
Prvn
  #2 (permalink)  
Old 11-18-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
Try this:


Code:
awk '
/^AAA/{s=$0}
/^BBB/ && s {print s "\n" $0; s=""}
' file

Regards
  #3 (permalink)  
Old 11-18-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
Thank you Franklin.

Your solution worked great!


Prvn
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0