extract lines based on few conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract lines based on few conditions
# 1  
Old 10-16-2008
extract lines based on few conditions

Hi,

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

File format details:
notes:
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, if any BBB's 5th column is blank then then script should provide us it's AAA line.

For. e.g, i have data for 3 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,,m

then the output must be

AAA,3,w,x,y,z

I could extract the line that is missing by below:

gawk -F"," '$1 == "BBB" && $5 == "" { print $0 }' /tmp/file

but clueless how to get the AAA-line of it.

Please advise.


Thanks
Prvn
# 2  
Old 10-16-2008
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk -F, '/^AAA/ { _ = $0 }
/^BBB/ && !$5 { print _; _ = "" }
' infile

If the 5th field may contain 0 you should use:

Code:
$5==""

instead of:

Code:
!$5

# 3  
Old 10-16-2008
Thanks radoulov,

Your Solution worked like a charm.

Could you please tell me what does _ = "" [ after { print _; ] do?


Thanks
Prvn
# 4  
Old 10-16-2008
Quote:
Originally Posted by prvnrk
Thanks radoulov,

Your Solution worked like a charm.

Could you please tell me what does _ = "" [ after { print _; ] do?
[...]
Given your input format it's unnecessary.
You can remove it Smilie

Code:
awk -F, '/^AAA/ { _ = $0 }
/^BBB/ && !$5 { print _ }
' infile

# 5  
Old 10-17-2008
Code:
awk -F"," '{
if($1=="AAA")
        tmp=$0
if($1=="BBB" && $5=="")
        print tmp
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract some characters from lines based on pattern

Hi All, i would like to get some help regarding extracting certain characters from a line grepped. blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah... (10 Replies)
Discussion started by: mad man
10 Replies

2. Shell Programming and Scripting

Extract specific lines based on another file

I have a folder containing text files. I need to extract specific lines from the files of this folder based on another file input.txt. How can I do this with awk/sed? file1 ARG 81.9 8 81.9 0 LEU 27.1 9 27.1 0 PHE .0 10 .0 0 ASP 59.8 11 59.8 0 ASN 27.6 12 27.6 0 ALA .0 13 .0 0... (5 Replies)
Discussion started by: alanmathew84
5 Replies

3. Shell Programming and Scripting

Extract duplicate rows with conditions

Gents Can you help please. Input file 5490921425 1 7 1310342 54909214251 5490921425 2 1 1 54909214252 5491120937 1 1 3 54911209371 5491120937 3 1 1 54911209373 5491320785 1 ... (4 Replies)
Discussion started by: jiam912
4 Replies

4. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Extract lines between tags and conditions

Hi forum, I need help .. I need to extract from the log file logfile.log all the rows between the tags "<SOAP-ENV: Envelope" "</ SOAP-ENV: Envelope>" and that contain a certain <idPrescrizione> 0FXTN091GI </ idPrescrizione>. Within logfile.log there could be more 'occurrences of the same ... (3 Replies)
Discussion started by: mannone
3 Replies

6. Shell Programming and Scripting

Extract paragraphs under conditions

Hi all, I want to extract some paragraphs out of a file under certain conditions. - The paragraph must start with 'fmri' - The paragraph must contain the string 'restarter svc:/system/svc/restarter:default' My input is like that : fmri svc:/system/vxpbx:default state_time Wed... (4 Replies)
Discussion started by: Armoric
4 Replies

7. Shell Programming and Scripting

Report generation based on certain conditions

Hi I recently joined a project where I have been asked to generate a report using shell script accessing UNIX box. I have no idea on how to do it as I am a beginner and learning shell scripts. Suppose I have a XML: Code: ... (3 Replies)
Discussion started by: vat1kor
3 Replies

8. Shell Programming and Scripting

Extract file records based on some field conditions

Hello Friends, I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited): ColA|ColB|ColC|ColD|ColE|ColF Now for this file, I have to get those records which fulfil the following condition: If "ColB" is NOT NULL and "ColD" has values one of the following... (9 Replies)
Discussion started by: mehimadri
9 Replies

9. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

10. Shell Programming and Scripting

validating a file based on conditions

i have a file in unix in which the records are like this aaa 123 233 aaa 234 222 aaa 242 222 bbb 122 111 bbb 122 123 ccc 124 222 In the output i want only the below records aaa ccc The validation logic is 1st column and 2nd column need to be considered if both columns values are... (8 Replies)
Discussion started by: trichyselva
8 Replies
Login or Register to Ask a Question