Doubt in this trivial awk code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doubt in this trivial awk code
# 1  
Old 04-14-2011
Doubt in this trivial awk code

Hi,

What is the difference in the following two awk one-liners?
Code:
awk -F, '{s[$2]++} END {if (s[$2] == 1 && $4 > "09:10:00") {print $2, $4}}' f1
awk -F, '{s[$2]++} s[$2] == 1 && $4 > "09:10:00" {print $2, $4}' f1

Even though, all the 2nd column values have duplicate records, the first code does not give any output whereas the second one gives some output. Could anyone please explain the process flow?

This is the data file for which I am working upon:
Code:
01,02877,2011-01-26,08:29:00,IN
01,02877,2011-01-26,17:11:00,OUT
01,05713,2011-01-26,08:11:00,IN
01,05713,2011-01-26,13:47:00,OUT
01,05713,2011-01-26,14:47:00,IN
01,05713,2011-01-26,17:08:00,OUT
01,06771,2011-01-26,09:08:00,IN
01,06771,2011-01-26,18:27:00,OUT
01,15872,2011-01-26,08:08:00,IN
01,15872,2011-01-26,08:57:00,OUT
01,15872,2011-01-26,09:06:00,IN
01,15872,2011-01-26,13:28:00,OUT
01,15872,2011-01-26,13:37:00,IN
01,15872,2011-01-26,15:50:00,OUT
01,15872,2011-01-26,16:01:00,IN
01,15872,2011-01-26,17:18:00,OUT
01,22272,2011-01-26,09:34:00,IN
01,22272,2011-01-26,18:27:00,OUT
01,22671,2011-01-26,08:57:00,IN
01,22671,2011-01-26,16:58:00,OUT
01,29670,2011-01-26,08:27:00,IN
01,29670,2011-01-26,12:08:00,OUT
01,29670,2011-01-26,12:15:00,IN
01,29670,2011-01-26,13:50:00,OUT
01,29670,2011-01-26,14:05:00,IN
01,29670,2011-01-26,16:26:00,OUT
01,29670,2011-01-26,16:32:00,IN
01,29670,2011-01-26,18:38:00,OUT
01,29972,2011-01-26,09:20:00,IN
01,29972,2011-01-26,11:32:00,OUT
01,29972,2011-01-26,12:20:00,IN
01,29972,2011-01-26,13:52:00,OUT

# 2  
Old 04-14-2011
Quote:
Originally Posted by royalibrahim
Hi,

What is the difference in the following two awk one-liners?
Code:
awk -F, '{s[$2]++} END {if (s[$2] == 1 && $4 > "09:10:00") {print $2, $4}}' f1
awk -F, '{s[$2]++} s[$2] == 1 && $4 > "09:10:00" {print $2, $4}' f1

Even though, all the 2nd column values have duplicate records, the first code does not give any output whereas the second one gives some output. Could anyone please explain the process flow?
The 1st will count occurrences of $2 values but only print if $2 has occurred once and the final $4 is larger than "09:10:00".

The 2nd will also count occurrences of $2 values, but will print from each line if $2 has only occurred once so far and the current $4 is larger than "09:10:00".
# 3  
Old 04-14-2011
The first command is totally wrong, after END, only the last record is used for condition compare.

The second one is fine, but can be shorter:

Code:
awk -F, '!a[$2]++ && $4 > "09:10:00" {print $2, $4}'  f1

# 4  
Old 04-15-2011
Quote:
Originally Posted by kato
The 1st will count occurrences of $2 values but only print if $2 has occurred once and the final $4 is larger than "09:10:00".

The 2nd will also count occurrences of $2 values, but will print from each line if $2 has only occurred once so far and the current $4 is larger than "09:10:00".
It looks to me both of your statements meaning the same.

But none of the 2nd field records are free from duplicates. So I feel it should either return all the records or null. But it is not happening

---------- Post updated at 06:06 PM ---------- Previous update was at 06:02 PM ----------

Quote:
Originally Posted by rdcwayx
The first command is totally wrong, after END, only the last record is used for condition compare.
What do you mean by last record? I could not see such restriction in the awk manual.

Also, I have come across many awk codes having records comparing inside END, but not just the last record.
Quote:
Originally Posted by rdcwayx
The second one is fine, but can be shorter:

Code:
awk -F, '!a[$2]++ && $4 > "09:10:00" {print $2, $4}'  f1

Nice, but, could you please explain me what is happening here and how it differs from
Code:
awk -F, '{s[$2]++} END {if (s[$2] == 1 && $4 > "09:10:00") {print $2, $4}}' f1

????
# 5  
Old 04-15-2011
Quote:
Originally Posted by royalibrahim
It looks to me both of your statements meaning the same.

But none of the 2nd field records are free from duplicates. So I feel it should either return all the records or null. But it is not happening
Perhaps a better approach to solving your problem would be to tell us what you want to do with your data file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt on using AWK

DE_CODE|1{AXXANY}1APP_NAME|2{TELCO}2LOC|NY DE_CODE|1{AXXATX}1APP_NAME|2{TELCO}2LOC|TX DE_CODE|1{AXXABT}1APP_NAME|2{TELCO}2LOC|BT DE_CODE|1{AXXANJ}1APP_NAME|2{TELCO}2LOC|NJ i have out put file like below i have to convert it in the format as below. DE_CODE = AXXANY APP_NAME= TELCO LOC = NY... (4 Replies)
Discussion started by: mail2sant
4 Replies

2. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

Trivial perl doubt about FILE

Hi, In the following perl code: #!/usr/bin/perl -w if (open(FILE, "< in_file")) { while (<FILE>) { chomp($_); if ($_ =~ /patt$/) { my $f = (split(" ", $_)); print "$f\n"; } } close FILE; } Why changing the "FILE" as... (4 Replies)
Discussion started by: royalibrahim
4 Replies

4. Shell Programming and Scripting

doubt on awk

I have executed the below command: find . -name "Ks*" -type f -exec ls -ltr {} \; | awk '{printf("%ld %s %d %s \n",$5,$6,$7,$8,$9)}' and here is the output: 1282 Oct 7 2004 51590 Jul 10 2006 921 Oct 7 2004 1389 Jun 4 2003 1037 May 19 2004 334 Mar 24 2004 672 Jul 8 2003 977... (6 Replies)
Discussion started by: venkatesht
6 Replies

5. Programming

A trivial XOR doubt in a program

Hi, I am trying to reverse a string using the following program utilizing the Exclusive OR bit operation: int main() { char str = "Quraish"; char *p = str, temp; char *q = str + strlen(str) - 1; while ( p != q ) { if (*p != *q) { *p ^= *q; *q ^= *p; *p ^= *q;... (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Shell Programming and Scripting

Doubt in awk

Hi All, I have two files as given below: fileA 1234|aaaaa|vvvv 2222|bbbbbb|cbxxbjh 3333|cccc|jhjhj fileB 3434|bbbcc|cbxxbjh 1234|cat|bullet 3333|cccc|jhjhj I need a script that reads the first column from fileA (i.e 1234) and searches in fileB in the first parameter(i.e first... (7 Replies)
Discussion started by: jisha
7 Replies

7. Shell Programming and Scripting

AWK doubt

Hello people I have a doubt about awk... I´m using it to create a condition where I do not want to use the 0 (zero) value of a certain column. - This is the original file: string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

8. Shell Programming and Scripting

doubt in AWK

Hi all, column1 -------- 33 44 55 66 please provide the script using awk command to dispaly output 55. Help apperciated.. thanks, Nirmal (4 Replies)
Discussion started by: abnirmal
4 Replies

9. Shell Programming and Scripting

trivial awk question

i posted a reply the other day and needed an answer to this question while i was clarifyiing a few matter.. "how to compare to date variable in string format without having to compare word for word".. my reply was to try to use awk to compare the strings.. I wasn't quite sure if i remembered how... (2 Replies)
Discussion started by: moxxx68
2 Replies

10. UNIX for Dummies Questions & Answers

awk doubt

I'm having a file with 5 fields. I want to sort that file according to one field no 3. How shall I do using awk programming. Any input appreciatable. regards, vadivel. (7 Replies)
Discussion started by: vadivel
7 Replies
Login or Register to Ask a Question