Simplified awk script for if else statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simplified awk script for if else statements
# 1  
Old 10-03-2014
Simplified awk script for if else statements

Hi,

The below awk script that i did is working fine. It gives me the results that i want. But, the script is not smart and very long as i have 8 conditions to meet. The sample script below only show 2 conditions.

Code:
awk 'BEGIN{FS=OFS=" ~ |\t"} 
{if (($7>$9) && ($6>$8)){
	Ql= $7-$6; 
	Sl= $9-$8;	
	Hl = $9-$6;
	Qc= (Hl/Ql)*100;
	Sc= (Hl/Sl)*100;
	{if(Sl>Ql){
	Max = Sl;	
	Diff = ((Sl-Ql)/Max)*100;}
	else {
	Max = Ql;
	Diff = ((Ql-Sl)/Max)*100;}
	print $0"\t"Qc"\t"Sc"\t"Diff;}}
else if (($9>$7) && ($8<$6)){
	Ql= $7-$6; 
	Sl= $9-$8;
	Hl = $7-$6;
	Qc= (Hl/Ql)*100;
	Sc= (Hl/Sl)*100;
	{if(Sl>Ql){
	Max = Sl;	
	Diff = ((Sl-Ql)/Max)*100;}
	else {
	Max = Ql;
	Diff = ((Ql-Sl)/Max)*100;}
	print $0"\t"Qc"\t"Sc"\t"Diff;}}
else
	print "error";
}'

sample inputfile

Code:
174.14A.1	A10gh1	174.14A.1.11	22	24	489 ~ 1908	264 ~ 1654	75.03%	0.0	2448	ion channel (don) fam.
174.14A.1	A3g130	174.14A.1.50	5	6	75 ~ 656	33 ~ 672	94.81%	1e-126	1007	dressrossa don fam
201.1A.66	A17l50	201.1A.66.33	7	9	19 ~ 586	32 ~ 628	76.25%	1e-119	962	dressrossa don fam

Sample output
Code:
174.14A.1	A10gh1	174.14A.1.11	22	24	489 ~ 1908	264 ~ 1654	75.03%	0.0	2448	ion channel (don) fam.	82.10	83.81	2.04
174.14A.1	A3g130	174.14A.1.50	5	6	75 ~ 656	33 ~ 672	94.81%	1e-126	1007	dressrossa don fam	100.00	90.92	9.08
error

I don't have any problem with the output, all i want to know is much better way to simplify this in awk. Can anyone help/advise me about this. Thanks

Last edited by redse171; 10-03-2014 at 10:47 PM.. Reason: wrong output
# 2  
Old 10-03-2014
This seems to do the same and is somewhat shorter:
Code:
awk '
  BEGIN{
   FS=" ~ |\t"
   OFS="\t"
  }
  {
    if($7>$9 && $6>$8) 
      Hl=$9-$6
    else if ($9>$7 && $8<$6)
      Hl=$7-$6
    else {
      print "error"
      next
    }
    Ql=$7-$6
    Sl=$9-$8
    Qc=(Hl/Ql)*100
    Sc=(Hl/Sl)*100
    R=(S1>Q1) ? Ql/Sl : Sl/Ql
    Diff=(1-R)*100
    print $0, Qc, Sc, Diff
  }
' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-03-2014
I just noticed 2 mistakes :
1)
OFS=" ~ |\t" ==> useless as you don't use the comma for the print statement
2)
Code:
{if (($7>$9) && ($6>$8))...
then 
else if (($9>$7) && ($8<$6))

($6>$8) is the same as ($8<$6) ???
So why not create an overall condition with it ?


Jean-Paul

Last edited by Scrutinizer; 10-04-2014 at 02:10 AM.. Reason: mistakes I made; Mod: code tags
# 4  
Old 10-03-2014
Hi,
Another way:
Code:
awk 'BEGIN{FS=OFS=" ~ |\t"} 
($7>$9 && $6>$8 && Hl=$9-$6) || ($9>$7 && $8<$6 && Hl=$7-$6) {
	Ql= $7-$6; 
	Sl= $9-$8;	
	Qc= (Hl/Ql)*100;
	Sc= (Hl/Sl)*100;
	Diff=(Sl>Ql) ? ((Sl-Ql)/Sl)*100 : ((Ql-Sl)/Ql)*100
	print $0"\t"Qc"\t"Sc"\t"Diff;
	next;}
{print "error"}'

Your output is wrong, line 3 print error because $8>$6

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 10-03-2014
Quote:
Originally Posted by Scrutinizer
This seems to do the same and is somewhat shorter:
Code:
awk '
  BEGIN{
   FS=" ~ |\t"
   OFS="\t"
  }
  {
    if($7>$9 && $6>$8) 
      Hl=$9-$6
    else if ($9>$7 && $8<$6)
      Hl=$7-$6
    else {
      print "error"
      next
    }
    Ql=$7-$6
    Sl=$9-$8
    Qc=(Hl/Ql)*100
    Sc=(Hl/Sl)*100
    R=(S1>Q1) ? Ql/Sl : Sl/Ql
    Diff=(1-R)*100
    print $0, Qc, Sc, Diff
  }
' file

Hi Scrutinizer,

Thanks a lot! your code is a lot shorter and clean. but i have a problem with R=(S1>Q1) ? Ql/Sl : Sl/Ql . Some of the results give me negative values and i know why. So, i used my former code for this one to get the result that i want. But, i am curious to know about this line of code. Can u pls explain it? thanks
# 6  
Old 10-03-2014
R=(S1>Q1) ? Ql/Sl : Sl/Ql This statement makes use of the ternary operator evaluation ? :
Evaluates (S1>Q1) if is true uses the first portion after the `?' that is Ql/Sl, if is false uses the portion after the `:' Sl/Ql. In this case, whatever it gets executed is assigned into the variable R
This User Gave Thanks to Aia For This Post:
# 7  
Old 10-03-2014
Quote:
Originally Posted by Aia
R=(S1>Q1) ? Ql/Sl : Sl/Ql This statement makes use of the ternary operator evaluation ? :
Evaluates (S1>Q1) if is true uses the first portion after the `?' that is Ql/Sl, if is false uses the portion after the `:' Sl/Ql. In this case, whatever it gets executed is assigned into the variable R
Hi Aia,

great..thanks so much! Smilie

---------- Post updated at 10:04 PM ---------- Previous update was at 10:02 PM ----------

Quote:
Originally Posted by disedorgue
Hi,
Another way:
Code:
awk 'BEGIN{FS=OFS=" ~ |\t"} 
($7>$9 && $6>$8 && Hl=$9-$6) || ($9>$7 && $8<$6 && Hl=$7-$6) {
	Ql= $7-$6; 
	Sl= $9-$8;	
	Qc= (Hl/Ql)*100;
	Sc= (Hl/Sl)*100;
	Diff=(Sl>Ql) ? ((Sl-Ql)/Sl)*100 : ((Ql-Sl)/Ql)*100
	print $0"\t"Qc"\t"Sc"\t"Diff;
	next;}
{print "error"}'

Your output is wrong, line 3 print error because $8>$6

Regards.
Hi disedorgue,

Thanks for the info. Just fixed the sample output. Tried your codes and it works too. thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 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

AWK w/ if statements failing.

I'm converting some code from ksh on my macbook (Version M 1993-12-28 s+) to an older solaris machine with ksh 88. I can't seem to figure out this line, it worked on the new shell version. set -A combo -- $(for x in ${ImageIDs}; do nawk -v s=$x 'if($2 == s) getline ; getline if ($1 ==... (2 Replies)
Discussion started by: nerdcurious
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

Combining awk statements

I have a pretty simple script below: #!/bin/sh for i in *.cfg do temp=`awk '/^InputDirectory=/' ${i}` input_dir=`echo ${temp} | awk '{ print substr( $0, 16) }'` echo ${input_dir} done As you can see its opening each cfg file and searching for the line that has "InputDirectory="... (3 Replies)
Discussion started by: ssbsts
3 Replies

8. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies

9. Shell Programming and Scripting

Help a newbie please with awk if else statements

Hi, Despite reading the Conditional Statements chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot for the life of me get any kind of if ... else statement to work in my awk scripts! My scripts work perfectly (as they are written at least) and do what... (4 Replies)
Discussion started by: jonathanm
4 Replies

10. Shell Programming and Scripting

awk compound statements

how can i use two or multiple statements in the if part of an awk code for example i want to check two flag if they are true i will write some print operations and increase the counter. here is the c version of the code that i want to write: counter=0; if (flag1==1 && flag2==0) {... (7 Replies)
Discussion started by: gfhgfnhhn
7 Replies
Login or Register to Ask a Question