How to use awk or nawk by using Conditional Statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use awk or nawk by using Conditional Statements
# 1  
Old 08-24-2011
How to use awk or nawk by using Conditional Statements

Hi Guys!

Anybody know how can I use a nawk or awk on a script and printing the NAME, SECTION (must be 410 or 411 or 414) and TOTAL COST of CLASS 1 and 3 combined must be greater than 50. See below desired output file.


input.txt:
Code:
NAME,CLASS,COST,SECTION
JOHN,1,10,410
JOHN,2,20,410
JOHN,3,30,410
ABEL,1,20,411
ABEL,2,30,411
ABEL,3,70,411
BONO,1,30,412
BONO,2,10,412
BONO,3,20,412
CRIS,1,40,413
CRIS,2,20,413
CRIS,3,10,413
DINA,1,50,414
DINA,2,30,414
DINA,3,20,414
DINA,1,60,415
DINA,2,10,415
DINA,3,30,415



Desired output.txt:
Code:
ABEL,411,90
DINA,414,70


Thanks in advance!


Br,
pinpe
# 2  
Old 08-24-2011
Code:
awk -F, '
! ($4 == 410 || $4 == 411 || $4 == 414) || $2 == 2 { next }
$2 == 1 { a[$1] = $3 }
$2 == 3 && (t=a[$1]+$3) > 50 {print $1 "," $4 "," t}  
' INPUTFILE

# 3  
Old 08-24-2011
Code:
 
$ nawk -F, '{if($4~/41[014]/){if($2==1){a=$3}else{if($2==3 && ($3+a) >50){print $1,$4,$3+a;a=0}}}}' test
ABEL 411 90
DINA 414 70

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

2. Shell Programming and Scripting

awk with many if statements

Hi What is the right structure to use awk with multiple If statements The following code doesn't work # awk ' { A = $1 } END { for ( i = 1; i <= c; i++ ) { if ( A == 236 && A ==199... (7 Replies)
Discussion started by: khaled79
7 Replies

3. Shell Programming and Scripting

awk problem - combining awk statements

i have a datafile that has several lines that look like this: 2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4 using the following command: awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY}... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

5. UNIX for Dummies Questions & Answers

Conditional execution of statements

Hi , I have a script which has multiple awk and sed commands like below. First one :- find /root/src/ -name "*csv" -size 0 -exec rm {} \; Second one: - ls *SRE*.txt > SRT_TRAN.dat rm *asr*.txt Third one :- find /root/src/ -name '*.csv' | while read FILENAME ; do awk... (2 Replies)
Discussion started by: shruthidwh
2 Replies

6. Shell Programming and Scripting

Combining AWK statements

Hello UNIX Community, I have file that contains the following data: testAwk2.csv rabbit penguin goat giraffe emu ostrich hyena elephant panda dog cat pig lizard snake antelope platypus tiger cheetah lion rhino spider I then find the character length of the... (1 Reply)
Discussion started by: vnayak
1 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies

9. Shell Programming and Scripting

Conditional Statements

How can I compare two decimal values within a function using Bash? Function fun2 isn't comparing the decimal values. Is there a way to do this using Bash or Korn? #!/bin/bash set -x x=1 z=110 function fun1() { i=`bc << EOF 2>> /dev/null scale=3 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question