ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
July 2018,19%,46%
August 2018,20%,45%
September 2018,20%,41%
October 2018,21%,39%
November 2018,21%,39%
December 2018,21%,41%
January 2019,25%,46%
February 2019,27%,50%
I need to calculate the difference between the values in the second column but two by two:
Lookup the printf function in the AWK manual. "man awk"
The % is a special character, so to print just a % you have to write it as %%.
Normal variables are just plain names, they don't have $ in front, except for the field numbers.
So the m=$1 and u=$3 just save the first and third field for later as m and u respectively. The calculation is done the next time through with $3 - u, which subtracts the third field from the previous record from the third field in the current record.
The fields are separated by a pattern [ ,%], so a space or comma or percentage sign will mark the border between fields.
The body of the above AWK script could be expressed something like this in pseudo-code:
Code:
if(FNR==0) then {
next
}
if (FNR>1 ) then {
print m " → " $1;
printf ("CPU : %+d%%%s\n",$3-u,ORS);
}
if (true) then {
m=$1;
u=$3
}
Last edited by Turbocapitalist; 04-15-2019 at 11:58 AM.
printf:
the %+d refers to the first following argument ($3-u)
the %% is a % character.
The %s refers the second following argument (ORS).
ORS is "\n" (=linefeed) by default, so you can as well say: printf ("CPU : %+d%%\n\n", $3-u)
Lookup the printf function in the AWK manual. "man awk"
The % is a special character, so to print just a % you have to write it as %%.
Normal variables are just plain names, they don't have $ in front, except for the field numbers.
So the m=$1 and u=$3 just save the first and third field for later as m and u respectively. The calculation is done the next time through with $3 - u, which subtracts the third field from the previous record from the third field in the current record.
The fields are separated by a pattern [ ,%], so a space or comma or percentage sign will mark the border between fields.
The body of the above AWK script could be expressed something like this in pseudo-code:
Code:
if(FNR==0) then {
next
}
if (FNR>1 ) then {
print m " → " $1;
printf ("CPU : %+d%%%s\n",$3-u,ORS);
}
if (true) then {
m=$1;
u=$3
}
Quote:
Originally Posted by MadeInGermany
FNR==0 is never true. You can omit it.
printf:
the %+d refers to the first following argument ($3-u)
the %% is a % character.
The %s refers the second following argument (ORS).
ORS is "\n" (=linefeed) by default, so you can as well say: printf ("CPU : %+d%%\n\n", $3-u)
Hello
Thank you for those explanations ! I understand better this script !
One more thing I didn't really understand :
Where and how the values in the second column are selected ?
I mean, if I want to extract this colum with the CPU value, I make :
Code:
awk -F',' '{print $2}' CLUSTER_1.txt
And here I specify that I select the value 2. But with this script, I don't understand how the values of the second column are selected !
The secret sauce is in the input Field Separator (FS) as defined by the pattern set by -F. There are several ways to assign the Field Separator a value or a pattern. The -F option is one of them. So your pattern is any time one of the characters between the brackets is found [ ,%] awk would start a new field. That is to say whenever a space, a comma, or a percentage sign is found. So consider this line:
August 2018,20%,45%
With your pattern, "August" would be the first field, "2018" the second, "20" the third, and 45 the fifth. Yes, the fifth. Now if you modify the pattern to [ ,%]+ then that last one will be the fourth.
The secret sauce is in the input Field Separator (FS) as defined by the pattern set by -F. There are several ways to assign the Field Separator a value or a pattern. The -F option is one of them. So your pattern is any time one of the characters between the brackets is found [ ,%] awk would start a new field. That is to say whenever a space, a comma, or a percentage sign is found. So consider this line:
August 2018,20%,45%
With your pattern, "August" would be the first field, "2018" the second, "20" the third, and 45 the fifth. Yes, the fifth. Now if you modify the pattern to [ ,%]+ then that last one will be the fourth.
Thank you for these clarifications ! I finally understand how it works !
So, with this csv file:
Code:
July 2018 CLUSTER_ALE_01 19% 46%
August 2018 CLUSTER_ALE_01 20% 45%
September 2018 CLUSTER_ALE_01 20% 41%
October 2018 CLUSTER_ALE_01 21% 39%
November 2018 CLUSTER_ALE_01 21% 39%
December 2018 CLUSTER_ALE_01 21% 41%
January 2019 CLUSTER_ALE_01 25% 46%
February 2019 CLUSTER_ALE_01 27% 50%
July → August
CPU : +1%
RAM : -1%
August → September
CPU : +0%
RAM : -4%
September → October
CPU : +1%
RAM : -2%
October → November
CPU : +0%
RAM : +0%
November → December
CPU : +0%
RAM : +2%
December → January
CPU : +4%
RAM : +5%
January → February
CPU : +2%
RAM : +4%
We agree that "u" or the "o" I added are just variable names to receive the content of $1 and $3?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.