INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Multiple if with one else syntax

Multiple if with one else syntax

(OP)

CODE -->

gawk 'BEGIN {}
!/btvtracker/ { next }
{
split (substr ($12,50),a,"&");clickthru=a[1];etvchannel=a[2]
split (etvchannel,b,"=");chantype=b[1];channo=b[2]
x[channo","clickthru]++
}

END {
for (y in x) {
split (y,c,",");i=c[1];j=c[2]
vchanname=etvchannel

if (i=="101") { vchanname="BBC One"; }
if (i=="102") { vchanname="BBC Two" }
if (i=="203") { vchanname="ITV 1" }
if (i=="304") { vchanname="Channel 4" }
if (i=="405") { vchanname="Five" }
if (i=="506") { vchanname="BBC 3" }
if (i=="607") { vchanname="BBC 4 " }
if (i=="808") { vchanname="BBC ONE HD" }
# etc.......

#else { continue }
print i","vchanname","j","x[y]
}


}' ${FILE} | sort -n >> ${WEBDIR}/${SITE}.${REPDATE}.csv


The code above produces count of http hits from a large apache access log. It works but includes "i" numbers that I am not interested in which is why I am trying to use the "else" to filter out "i" numbers that I am not interested in. I have tried different permutations of placing the the brackets {} but cannot get the code to read the if statements as one block with the one following else. I have tried using "else if" as well but to no avail. Can someone please assist in providing the correct syntax?

For the more learned among you, I am not that keen on hard coding the TV names within the gawk program and would ideally like to have a seperate master file that held the names and the gawk program did a lookup to that file.

For example the master file would like this...
101,BBC One
102,BBC Two
203,ITV 1
304,Channel 4
405,Five
etc....

As always,

Thanks in advance

Madasafish

RE: Multiple if with one else syntax

Hi

Quote (Madasafish)

I have tried using "else if" as well but to no avail.
How ? That is what you need.

CODE --> )

if (i=="101") vchanname="BBC One"
else if (i=="102") vchanname="BBC Two"
else if (i=="203") vchanname="ITV 1"
else if (i=="304") vchanname="Channel 4"
else if (i=="405") vchanname="Five"
else if (i=="506") vchanname="BBC 3"
else if (i=="607") vchanname="BBC 4"
else if (i=="808") vchanname="BBC ONE HD"
else continue
But would be much better to avoid all those ifs by using an array :

CODE --> )

BEGIN {
chanlist["101"]="BBC One"
chanlist["102"]="BBC Two"
chanlist["203"]="ITV 1"
chanlist["304"]="Channel 4"
chanlist["405"]="Five"
chanlist["506"]="BBC 3"
chanlist["607"]="BBC 4"
chanlist["808"]="BBC ONE HD"
}

# then later in place of those ifs
if (i in chanlist) vchanname=chanlist[i]
else continue

Of course this is still ugly practice. You should store the channel list in a separate data file to avoid all those assignments to chanlist array items :

CODE --> )

BEGIN {
FS="\t"
while (getline < "channellist.txt")
chanlist[$1]=$2
close("channellist.txt")
FS=" "
}

# the checking part remains the same
if (i in chanlist) vchanname=chanlist[i]
else continue

CODE --> channellist.txt

101 BBC One
102 BBC Two
203 ITV 1
304 Channel 4
405 Five
506 BBC 3
607 BBC 4
808 BBC ONE HD
After this you can add/remove/change channels without the need to modify the Awk code.

Feherke.
http://feherke.github.com/

RE: Multiple if with one else syntax

(OP)
Thank-you Feherke for an excellent explanation, it worked a treat.

Can I take this one step further.....?

I want to include genre's as the 3rd column of the channellist.txt file. Please note I have changed the Field seperator to "," eg:

CODE -->

428,Film4,Movies
434,Sky Comedy,Comedy
511,Sky Sports,Sports
232,Nat Geo HD,Factual
etc...

I have tried variations of the getline section

CODE -->

chanlist[$1,$2,$3]=$1,$2,$3

If I use "print i,chanlist[i]" it does print out but not in the order I want.
When I try to split chanlist[i] and then print the split fields it does not work.
Ultimately, I am trying to print out...

print i","vchanname","j","x[y]","chanlist[$3]

If it is possible to access individual fields in a getline array.

As always thanks in advance,

Madasafish

RE: Multiple if with one else syntax

Hi

As I understand, your input data will still be that channel number/ID/code, so the chanlist array's indices should be the same $1.

Furthermore, the comma ( , ) is recognized ( and practically replaced with SUBSEP ) only in array subscript to emulate multidimensional arrays. So the commas in the right side values are syntactically invalid.

Given the above, the closest would be to store the data in a two dimensional array :

CODE --> (fragment)

chanlist[$1,"name"]=$2
chanlist[$1,"genre"]=$3
In case of "428,Film4,Movies" this would be syntactically equivalent with :

CODE --> (fragment)

chanlist[428,"name"]="Film4"
chanlist[428,"genre"]="Movies"
But because of the way comma is handled and multidimensional arrays are emulated, this would be practically equivalent with :

CODE --> (fragment)

chanlist["428\034name"]="Film4"
chanlist["428\034genre"]="Movies"
Not a joy to iterate over it.

If you have gawk 4 or newer, you can use real multidimensional array :

CODE --> gawk-4

chanlist[$1]["name"]=$2
chanlist[$1]["genre"]=$3
That way the iterating will not be compromised.

For a portable way, better use two arrays :

CODE --> any-awk

chanlist[$1]=$2
genrelist[$1]=$3

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close