conditional extracting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional extracting
# 1  
Old 11-18-2008
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  
Old 11-18-2008
Try this:

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

Regards
# 3  
Old 11-18-2008
Thank you Franklin.

Your solution worked great!


Prvn
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional IF Question

I trying to get a simple script to see if a directory contains any files in it. I am failing on the conditional IF statement. I am not sure if it because the command I am using is creating the variable as a string or if it is numeric or if I just have the syntax wrong. #!/usr/bin/ksh... (2 Replies)
Discussion started by: awkwardone
2 Replies

2. Shell Programming and Scripting

Conditional OR in shell

Hi I have been trying to write a simple code: if ] || ] then echo "Log Directory is not empty, we will continue with archive operation" else echo "Log Directory is empty, Exiting......." exit 1 fi It never checks for the second OR condition i.e. ] Could you please help? Thanks (8 Replies)
Discussion started by: ankur328
8 Replies

3. Shell Programming and Scripting

Conditional replacements

Hi, I have a requirement as below Input Jacuzzi,"Jet Rings, Pillows",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,, Jacuzzi,"Jet Rings, Pillows, Skirt/Apron",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,, Output Jacuzzi,"Jet Rings!@%... (6 Replies)
Discussion started by: kewk
6 Replies

4. UNIX for Dummies Questions & Answers

Conditional merge using vi

How do I do this in VI. I have a file with pattern like this.. pattern line line1 line2 line3 line4 pattern line line1 line2 line3 line4 pattern word line1 line2 line3 line4 whenever the word "pattern" is found in a line, I should delete that line and 2 lines below it & merge... (1 Reply)
Discussion started by: osbourneric
1 Replies

5. Shell Programming and Scripting

if conditional statement

Hi, I have a script like this: sample.sh mapping=$1 if then echo "program passed" fi I'm running the above script as ./sample.sh pass The script is not getting executed and says "integer expression expected" Could anyone kindly help me? (2 Replies)
Discussion started by: badrimohanty
2 Replies

6. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

7. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

8. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

9. Shell Programming and Scripting

AWK - conditional cause

Hello guys, I want to make a conditional cause in the following file using awk: awk '{ if ($2 != 0) print $1, $2, $3}' test.csv > test2.csv FILE EXAMPLE = test.csv string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101 ... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

10. Shell Programming and Scripting

conditional split

Hi, Can someone let me know how I can split a record when it contains a vairable length of fields. Line1 field1,field101,field102,field 103,field104 Line 2 field1,field101,field102,field 103,field104,field201,field202,field 203,field204 Line 3 field1,field101,field102,field... (5 Replies)
Discussion started by: braindrain
5 Replies
Login or Register to Ask a Question