Getting data from a flat file based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting data from a flat file based on condition
# 15  
Old 01-28-2014
This gives exit code 1 if 1 line of the file has a 0 in both the columns:
Code:
awk '{n=$6==0 && $7==0?1:n} END{if(n)exit 1}' file

And this gives exit code 1 if all the lines have a 0 in both the columns:
Code:
awk '{n=$6==0 && $7==0?n+1:n} END{if(n==NR)exit 1}' file

Make your choice. Smilie
# 16  
Old 01-28-2014
Quote:
Originally Posted by Vivekit82
Yes..no email should be sent and entire program should be skipped if both columns have 0 in all the lines.
I think he wants this, not sure
Code:
# All are zero
$ cat <<eod | awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{print f}' 
0,0
0,0
0,0
0,0
eod
1

# Not all are zero
$ cat <<eod | awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{print f}' 
0,0
0,0
0,0
0,1
eod
0

--edit---

Code:
$ awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{exit(f)}'

exit 1 if column6 and column7 are zero in all line, exit 0 if column6 and column7 are not zero in all line

Code:
$ awk 'BEGIN{s=1}s{f=($6==0 && $7==0)?1:0}f==0{s=0}END{exit(f)}'


Last edited by Akshay Hegde; 01-28-2014 at 03:50 PM.. Reason: typo
# 17  
Old 01-28-2014
Thnaks..I tried the soln proposed by Corona and Franklin but still i am getting the mail if column 6 and cloumn 7 both are 0 in file.
I want that if in any row of the file (either or both) of cloumn 6 and cloumn 7 are non zero then only mail should come.
Mail should not come only if all the rows in both cloumn have 0 values( only 1 case..)

case 1
0,0
0,0
0,0

No mail should be sent

Case 2
0,1
0,0
0,0

Mail should be sent

Case 3
0,0
1,0
0,1
Mail should be sent and so on.
# 18  
Old 01-28-2014
Quote:
Originally Posted by Vivekit82
Thnaks..I tried the soln proposed by Corona and Franklin but still i am getting the mail if column 6 and cloumn 7 both are 0 in file.
I want that if in any row of the file (either or both) of cloumn 6 and cloumn 7 are non zero then only mail should come.
Mail should not come only if all the rows in both cloumn have 0 values( only 1 case..)

case 1
0,0
0,0
0,0

No mail should be sent

Case 2
0,1
0,0
0,0

Mail should be sent

Case 3
0,0
1,0
0,1
Mail should be sent and so on.
codetags please

Code:
$ cat <<case1 | awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{exit(f)}';echo $?
0,0
0,0
0,0
case1

1

$ cat <<case2 | awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{exit(f)}';echo $?
0,1
0,0
0,0
case2

0

$ cat <<case3 | awk -F, 'BEGIN{s=1}s{f=($1==0 && $2==0)?1:0}f==0{s=0}END{exit(f)}';echo $?
0,0
1,0
0,1
case3

0

# 19  
Old 01-28-2014
So you always want mail, unless the file is entirely zeroes in those columns? How about a running count then:

Code:
awk '{ T += $6 + $7 } END { exit(!T) }' inputfile

T is the count of ones. If it's zero, it exits with error status(should stop the if-statement), if it's nonzero it exits with success(should run the if statement).
# 20  
Old 01-28-2014
i tried the code suggested by Corona,case 1 is success but in others also i am not getting the mail.
Code:
 
Case 1
0,0
0,0
0,0

No mail..success

Code:
 
Case 2:
0,0
0,1
0,0

No Mail..Failed..We should get a mail in this case.
# 21  
Old 01-28-2014
Quote:
Originally Posted by Corona688
So you always want mail, unless the file is entirely zeroes in those columns? How about a running count then:

Code:
awk '{ T += $6 + $7 } END { exit(!T) }' inputfile

T is the count of ones. If it's zero, it exits with error status(should stop the if-statement), if it's nonzero it exits with success(should run the if statement).
Nice approach corona Smilie didn't come in my mind Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

2. Shell Programming and Scripting

Dynamically prepare the condition using flat file sdata

I have a file in which the data looks like A,B My output should be T1.A=T2.A AND T1.B=T2.B If my input is A,B,C My output should be T1.A=T2.A AND T1.B=T2.B AND T1.C=T2.C Currently I am automating my databae script and strucked with this part.Not able to search the... (2 Replies)
Discussion started by: bikky6
2 Replies

3. Shell Programming and Scripting

Sort based on positions in flat file

Hello, For example: 12........6789101112..............20212223242526..................50 ( Positions) LName FName DOB (Lastname starts from 1 to 6 , FName from 8 to 15 and date of birth from 21 to29) CURTIS KENNETH ... (5 Replies)
Discussion started by: duplicate
5 Replies

4. UNIX for Dummies Questions & Answers

Deleting the unwanted data based on condition

hi i have my input data like this aaa bbb ccc asa dff nmj mnj saa dff oik aax cdx saa oik asq sdf dssi want my output file to be like this mnj saa dff oik aax cdx saa oiki want to retain only those lines which will have oik just below them and i want oik to be as next column to those... (1 Reply)
Discussion started by: anurupa777
1 Replies

5. Shell Programming and Scripting

To read a flat file containing XML data

I have a file something like this:aaaa.xml content of the file is 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <storeInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s> <BRANCH_NO>3061</BRANCH_NO> <BRANCH_NAME>GREEN EXPRESS</BRANCH_NAME> ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

6. UNIX for Dummies Questions & Answers

Normalize Data and write to a flat file

All, Can anyone please help me with the below scenario. I have a Flat file of the below format. ID|Name|Level|Type|Zip|MAD|Risk|Band|Salesl|Dealer|CID|AType|CValue|LV|HV|DCode|TR|DU|NStartDate|UserRole|WFlag|EOption|PName|NActivationDate|Os|Orig|Cus|OType|ORequired|DType 03|... (10 Replies)
Discussion started by: sp999
10 Replies

7. Shell Programming and Scripting

Load data from a flat file to oracle.

I have a flat file with records like Header 123 James Williams Finance2000 124 Pete Pete HR 1500 125 PatrickHeather Engg 3000 Footer The structure is: Eno:4 characters Name:8 characters Surname : 9 characters Dept:7 characters Sal:4characters These are sample... (1 Reply)
Discussion started by: Shivdatta
1 Replies

8. Shell Programming and Scripting

Shell script to email based on flat file output

Hi All, I am a newbee in unix but still have written a shell script which should trigger a mail based on certain conditions but the problem is that my file is not being read. Below is the code please advise. I do not know where is it failing. Note $ and the no followed with it is the no of... (1 Reply)
Discussion started by: apoorva
1 Replies

9. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question