awk not clear


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk not clear
# 1  
Old 10-18-2018
awk not clear

can anyone tell me the working of this logic.
here
Code:
"last"

is a variable or it has it own meaning . getting confused.

Code:
awk '{if (last && $2 != last) {print last, sum; sum=0} 
sum=sum+ $3; last = $2}
 END {print last, sum}'

# 2  
Old 10-18-2018
There can be various ways to escape confusion:


- read the man page to find out about "reserved words"
- is the questionable identifier assigned a value, or (meaningfiully!) used without assignment (as e.g. the NF or NR awk builtin variables)
- use another identifier, e.g. LAST or xyz, and see if the execution is the same
# 3  
Old 10-18-2018
This is the main loop that is run for each input record.
The variable "last" is tested for a value that was assigned in a previous loop cycle.
What happens in the first loop cycle? "last" is undefined, and is casted to a 0 (false) by the (boolean) context.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-18-2018
Hopefully it's correct Smilie

Code:
{
# On lines that meet condition -> if 'last' has a value and second field value of input is not equal to 'last'
if (last && $2 != last) {
        # print variables 'last' and 'sum', set 'sum' to zero
        print last, sum; sum=0
        }
# On each line 
        # define variable 'sum' with the current value of 'sum' incremented by the value third field of input
        # define variable 'last' as the value of second field.
        sum=sum+ $3; last = $2
}
END {
# print final values of 'last' and 'sum', after entire file is processed.
print last, sum
}

Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 5  
Old 10-23-2018
thx MadeInGermany and Peasant
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

how to clear particular port

Hi Everyone, How to check the status of the particular port and also clear it if it is locked without reboot? Please suggest. Thank you. (6 Replies)
Discussion started by: laxmikant
6 Replies

2. UNIX for Advanced & Expert Users

Ho to clear a MQ queue :

Hi All, Can anyone tell me how to clear list of queues in a file? My file FILE1 has 3 queues FILE1 FirstQueue SecondQueue ThirdQueue I want to clear all these queues which belong to the same Qmanager from another script... Can anyone help me for this.. Thanks in... (0 Replies)
Discussion started by: Ch Bushu
0 Replies

3. Shell Programming and Scripting

How to clear a file

Hello, I have a script which creates a certain text file. Whenever I call it, I need to recreate this file, because I have no need in the previous content. So I thought to remove the file every time I call the script, and that way I am sure that the previous content will not interrupt me.... (2 Replies)
Discussion started by: shira
2 Replies

4. Linux

SED/AWK Script to clear log file using timestamp?

I have a log file on our system which fills up with lines that have been timestamped, as follows.... 03/03/2008 10:56:06:815] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:06:820] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:07:003]... (2 Replies)
Discussion started by: davesimm
2 Replies

5. Shell Programming and Scripting

Clear Case, Awk and script

Hello. I should have asked this awhile ago but here is my situation. My task is to generate LOC for different directories. I have a text file that has dates in this format (01-Aug-2006). My task is to read each line and compare it to a branch date. Depending on the date, it should generate a... (0 Replies)
Discussion started by: mastachef
0 Replies

6. UNIX for Dummies Questions & Answers

Clear confusion

Hi, In some machines when i type "clear" it completely clears all the contents on that window but on some it simply scrolls up all the content. How can i change this? (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

7. Shell Programming and Scripting

tput clear

What is the difference between these two commands? tput clear /usr/bin/clear (4 Replies)
Discussion started by: whatisthis
4 Replies

8. UNIX for Dummies Questions & Answers

Please help me clear up some confusion

Hello All, I like this forum btw, and have only been lurking for about a day. Recently I purchased some new hardware (AMD Athlon 64 3200+ and a Asus K8V Deluxe Motherboard), and I want to find an OS that can take advantage of the 64 bit processor. Basically, what are the differences... (2 Replies)
Discussion started by: RoY_mUnSoN
2 Replies

9. UNIX for Dummies Questions & Answers

clear memory

Hi... how can i clear the Memory on AIX 4.3.3 without rebooting (like flush memory on oracle database ) THANX fenomen (2 Replies)
Discussion started by: fenomen
2 Replies

10. Programming

clear screen

what is the syntax for clearing the screen in c ? when i tried "Clrscr()" the CC complier does not reconise it. please do tell me more about this. thanking you imma (6 Replies)
Discussion started by: immanuelgangte
6 Replies
Login or Register to Ask a Question