1st time awk user strings not matching right....


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers 1st time awk user strings not matching right....
# 1  
Old 02-13-2016
Question 1st time awk user strings not matching right....

So I was given a file,and I want to count how many occurrences happen with a specific string. I have two, that could have up to 3 different outcomes.
Now my trouble I believe starts with this string, "news.cais.net"
but why?
as of now my output is this...
accepted rejected canceled
swrinde: 109325 5527 13418
news.cais.net: 0 0
but I cant figure out why the loop structure is not working with news.cais.net? any advice will help and thank you for taking the time to review my request.




Code:
BEGIN{ swrindePlus = 0
       swrindeMin  = 0
       can = 0
       newsPlus = 0
       newsMin  = 0
}
{
if(     $5 = "swrinde"   && $4     =  /+/ )  swrindePlus++
else if($5 = "swrinde"   && $4     = /\-/ )  swrindeMin++
else if($5 = "news.cais.net" && $4 = /\+/  ) newsPlus++
else if($5 = "news.cais.net" && $4 = /\-/  ) newsMin++
else if($5 = "swrinde"   && $4     = /\c/  ) can++


}
 END{
        print "         accepted        rejected        canceled"
#       printf"%-7s %-7s \n"  swrindePlus, swrindeMin, can 

        print "swrinde:  "   swrindePlus ,"        ",  swrindeMin, "      ", can
        print "news.cais.net:  "  newsPlus ,"           ", newsMin

}

# 2  
Old 02-13-2016
Not sure I understand your problem. Posting some representative sample always helps!

For the comparison, use the == operator, not the assignment = .
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-13-2016
And either use string comparison:
Code:
$4 == "+"

or
a regex match:
Code:
$4 ~ /\+/

Although there do not appear to be + or - signs in the sample that you posted.
Perhaps you mean to use a numerical test ( >0 or <0 ) ?

Also, in this case the BEGIN section is superfluous and you can use the associative quality of an array instead of several variables..
# 4  
Old 02-13-2016
Quote:
Originally Posted by Scrutinizer
And either use string comparison:
Code:
$4 == "+"

or
a regex match:
Code:
$4 ~ /\+/

Although there do not appear to be + or - signs in the sample that you posted.
Perhaps you mean to use a numerical test ( >0 or <0 ) ?

Also, in this case the BEGIN section is superfluous and you can use the associative quality of an array instead of several variables..
I will investigate the arrays. Thank you all for your input.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

(g)awk: Matching strings from one file in another file between two strings

Hello all, I can get close to what I am looking for but cannot seem to hit it exactly and was wondering if I could get your help. I have the following sample from textfile with many thousands of lines: File 1 PS001,001 HLK PS002,004 L<G PS004,002 XNN PS004,006 BVX PS004,006 ZBX=... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

Output counts of all matching strings lessthan a number using awk

The awk below is supposed to count all the matching $5 strings and count how many $7 values is less than 20. I don't think I need the portion in bold as I do not need any decimal point or format, but can not seem to get the correct counts. Thank you :). file chr5 77316500 77316628 ... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk extract strings matching multiple patterns

Hi, I wasn't quite sure how to title this one! Here goes: I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Discussion started by: chrissycc
8 Replies

4. Solaris

System time and Cron time stamp not matching

On Solaris 10 server the system date won't match with the timestamp on files created by a cron jobs, Please help here is what i get when i check for system date infodba-ie10ux014:/tcpdv1_ie10/tcadmin/bin\n\r-> date Tue Apr 24 15:27:43 GMT 2012at same time i executed a cron job, and checked... (4 Replies)
Discussion started by: karghum
4 Replies

5. Programming

help with matching strings

In C programming how do i check if a char is equal to a vowel , like a e i o or u, small or big case. in my function i have the parameter like *word, and i am using word in a for loop, to check if its equal. i use tolower(word)=='a' || ..... but for some reason it only matches on lower case and... (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

Strings not matching

Hi, I have got two variables holding strings, if i echo them, they print the same value but if i compare the condition fails?? can somebody suggest something?? I have checked the word count too, they are also same. Thanks, Atul (4 Replies)
Discussion started by: atulmt
4 Replies

7. UNIX for Dummies Questions & Answers

How to get a compiler gcc, cc, cl.exe...? (literally 1st time unix user)

Hi all, I've done hours of reading on google and can't find an answer... I'm trying to compile/install the Apache web server on a Unix vm. When i try ./compile (the file's been unzipped already), I get this error: $ ./configure checking for chosen layout... Apache checking for working mkdir... (8 Replies)
Discussion started by: eddlo
8 Replies

8. Shell Programming and Scripting

AWK- delimiting the strings and matching the fields

Hello, I am newbie in awk. I have just started learning it. 1) I have input file which looks like: {4812 4009 1602 2756 306} {4814 4010 1603 2757 309} {8116 9362 10779 } {10779 10121 9193 10963 10908} {1602 2756 306 957 1025} {1603 2757 307} and so on..... 2) In output: a)... (10 Replies)
Discussion started by: kajolo
10 Replies

9. Shell Programming and Scripting

awk scripting - matching records and summing up time

Hello. I just found out about awk, and it appears that this could handle the problem I'm having right now. I first stumbled on the thread How to extract first and last line of different record from a file, and that problem is almost similar to mine. In my case, an ASCII file will contain the... (0 Replies)
Discussion started by: Gonik
0 Replies

10. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies
Login or Register to Ask a Question