awk with many if statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with many if statements
# 1  
Old 05-29-2013
awk with many if statements

Hi

What is the right structure to use awk with multiple If statements

The following code doesn't work

Code:
#
awk '
        {
                A[++c] = $1
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        
 if ( A[i] == 236 && A[i+1] ==199 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==199)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==218 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==237 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==227 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==225 && A[i-1] ==222)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==229 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==199 && A[i-1] ==199)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==207 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==237 && A[i-1] ==209)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==207 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==237 && A[i-1] ==209)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==195 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==199 && A[i-1] ==227)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==228 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==225 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==202 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==218)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==225)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==237 && A[i-1] ==199)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==212 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==209)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==237)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==230 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==210)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==222 && A[i-1] ==199)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==199 && A[i-1] ==230)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==207 && A[i-1] ==218)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==225 && A[i-1] ==225)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==211 && A[i-1] ==32)  A[i] = 200
elif ( A[i] == 236 && A[i+1] ==220 && A[i-1] ==194)  A[i] = 203
elif ( A[i] == 236 && A[i+1] ==209 && A[i-1] ==200)  A[i] = 203

                        elif ( A[i] )
                               print A[i]
							   fi
                }
        }
' $1>"$1"normal


Thanks
Khaled
# 2  
Old 05-29-2013
awk does not have an elif keyword. Use nested if's if that's what you want.

Regards,
Alister
# 3  
Old 05-29-2013
This may be fixed by putting all the value into an array from an variable then compare one and one in a loop.

If you post what you are trying to do and the criteria for it, we may be able to help you.
# 4  
Old 09-23-2013
Hope you can use '||' symbol instead of multipl if statements as u r checking for one position values only
# 5  
Old 09-23-2013
Many nested if's look ugly.
I suggest to use continue to jump to the next loop cycle
Code:
...
                 for ( i = 1; i <= c; i++ )
                {
                        
if ( A[i] == 236 && A[i+1] ==199 && A[i-1] ==32)  {A[i] = 200; continue}
if ( A[i] == 236 && A[i+1] ==32 && A[i-1] ==199)  {A[i] = 200; continue}
if ( A[i] == 236 && A[i+1] ==218 && A[i-1] ==32)  {A[i] = 200; continue}
if ( A[i] == 236 && A[i+1] ==237 && A[i-1] ==32)  {A[i] = 200; continue}
...

# 6  
Old 09-23-2013
Something like this might be a bit more readable and maintainable.

Code:
awk ' BEGIN {
  A[236,199, 32]=200
  A[236, 32,199]=200
  A[236,218, 32]=200
  A[236,237, 32]=200
  A[236,227, 32]=200
  A[236,225,222]=200
  A[236,229, 32]=200
  A[236,199,199]=200
  A[236,207, 32]=200
  A[236,237,209]=200
  A[236,207, 32]=200
  A[236,237,209]=200
  A[236,195, 32]=200
  A[236,199,227]=200
  A[236,228, 32]=200
  A[236,225, 32]=200
  A[236,202, 32]=200
  A[236, 32,218]=200
  A[236, 32,225]=200
  A[236,237,199]=200
  A[236,212, 32]=200
  A[236, 32,209]=200
  A[236, 32,237]=200
  A[236,230, 32]=200
  A[236, 32,210]=200
  A[236,222,199]=200
  A[236,199,230]=200
  A[236,207,218]=200
  A[236,225,225]=200
  A[236,211, 32]=200
  A[236,220,194]=203
  A[236,209,200]=203
}
{ A[++c] = $1 }
END {
   for(i=1;i<c;i++)
      if((A[i] SUBSEP A[i+1] SUBSEP A[i+2]) in B) A[i]=B[A[i],A[i+1],A[i+2]]
}

# 7  
Old 09-24-2013
Quote:
Originally Posted by Chubler_XL
Something like this might be a bit more readable and maintainable.

Code:
awk ' BEGIN {
  A[236,199, 32]=200
  A[236, 32,199]=200
  A[236,218, 32]=200
  A[236,237, 32]=200
  A[236,227, 32]=200
  A[236,225,222]=200
  A[236,229, 32]=200
  A[236,199,199]=200
  A[236,207, 32]=200
  A[236,237,209]=200
  A[236,207, 32]=200
  A[236,237,209]=200
  A[236,195, 32]=200
  A[236,199,227]=200
  A[236,228, 32]=200
  A[236,225, 32]=200
  A[236,202, 32]=200
  A[236, 32,218]=200
  A[236, 32,225]=200
  A[236,237,199]=200
  A[236,212, 32]=200
  A[236, 32,209]=200
  A[236, 32,237]=200
  A[236,230, 32]=200
  A[236, 32,210]=200
  A[236,222,199]=200
  A[236,199,230]=200
  A[236,207,218]=200
  A[236,225,225]=200
  A[236,211, 32]=200
  A[236,220,194]=203
  A[236,209,200]=203
}
{ A[++c] = $1 }
END {
   for(i=1;i<c;i++)
      if((A[i] SUBSEP A[i+1] SUBSEP A[i+2]) in B) A[i]=B[A[i],A[i+1],A[i+2]]
}

In the END clause, you reference arrays A[x], and B[w,x,y] but you never define B. I assume that you intended for all of the A[w,x,y]=z statements in the BEGIN clause to define the array B[] instead of A[].

Note also that (like the elif's in the original post in this thread) the lines in red duplicate the two lines before that.

To match the elif's in the original post, shouldn't:
Code:
      if((A[i] SUBSEP A[i+1] SUBSEP A[i+2]) in B) A[i]=B[A[i],A[i+1],A[i+2]]

be:
Code:
      if((A[i] SUBSEP A[i+1] SUBSEP A[i-1]) in B) A[i]=B[A[i],A[i+1],A[i-1]]

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

4. Shell Programming and Scripting

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: NAME,CLASS,COST,SECTION JOHN,1,10,410 JOHN,2,20,410... (2 Replies)
Discussion started by: pinpe
2 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