LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2018, 10:01 PM   #1
LYC
Member
 
Registered: Jan 2014
Posts: 107

Rep: Reputation: Disabled
nawk queries


Hi,

Refer to the nawk script below running from the command line, I don't understand the part "$5 == "Dec" && last != "Dec"" and the part "last = $5". Can anyone enlighten me ?


$ last | nawk 'BEGIN {y='`date +%Y`'}$5 == "Dec" && last != "Dec" { y-- }{ print y,$0 ; last = $5 }' |more
 
Old 05-16-2018, 10:33 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Awk is a bunch of if-then statements with the ifs implied.

Code:
BEGIN {y='`date +%Y`'}

$5 == "Dec" && last != "Dec" { y-- }
{ print y,$0 ; last = $5 }
If you ignore the BEGIN {} clause, which is run just once right before the input data is read, you have two if statements. The first one { y-- } triggers when the fifth field ($5) is the string "Dec" and the variable "last" isn't the string "Dec". The second if statement triggers on every line and one of the things it does is assign the value of the fifth field ($5) to the variable "last". Then awk reads in the next line and starts at the top, not counting the BEGIN {} clause.

However I suspect there is an error in the BEGIN {} clause with the quoting.
 
Old 05-16-2018, 11:04 PM   #3
LYC
Member
 
Registered: Jan 2014
Posts: 107

Original Poster
Rep: Reputation: Disabled
In { print y,$0 ; last = $5 }, what is the meaning of ";" ?
 
Old 05-16-2018, 11:08 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Quote:
Originally Posted by LYC View Post
In { print y,$0 ; last = $5 }, what is the meaning of ";" ?
It's separator to mark the end of a statement. It's optional for the last statement in a clause and it can be replaced with a new line just as well.

Code:
{ 
        print y,$0
        last = $5 
}
Think of that in terms of the following pseudo-code:

Code:
IF [ always_true() ] THEN { 
        print y,$0; 
        last = $5;
}
See "man awk" or "man nawk" for the langauge reference manual.
 
Old 05-16-2018, 11:17 PM   #5
LYC
Member
 
Registered: Jan 2014
Posts: 107

Original Poster
Rep: Reputation: Disabled
Smile

Thanks
 
Old 05-17-2018, 12:47 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,293

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
I think it should compute the missing year in the output from the "last" commamd.
There is a bug: if the first record has "Dec", the variable last is yet empty, so the y-- is executed.
Fix:
Code:
last | nawk -v y=`date +%Y` '$5 == "Dec" && last != "Dec" && NR > 1 { y-- } { print y,$0 ; last = $5 }'
Also changed to the "normal" argument passing via -v
--
Maybe still buggy. The current month should also be considered.

Last edited by MadeInGermany; 05-17-2018 at 01:05 AM.
 
Old 05-17-2018, 01:52 AM   #7
LYC
Member
 
Registered: Jan 2014
Posts: 107

Original Poster
Rep: Reputation: Disabled
Noted
 
Old 05-17-2018, 04:31 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,293

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
I think I got it now: simply initialize the "last" variable with the current month!
Code:
last | nawk -v y=`date +%Y` -v last=`date +%b` '$5 == "Dec" && last != "Dec" { y-- } { print y,$0 ; last = $5 }'
 
Old 05-17-2018, 08:54 PM   #9
LYC
Member
 
Registered: Jan 2014
Posts: 107

Original Poster
Rep: Reputation: Disabled
Thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
nawk in linux joyrock Programming 3 02-10-2010 10:28 AM
Need help with nawk ryanlum Linux - General 3 04-07-2008 05:02 PM
Nawk output omega71122 Solaris / OpenSolaris 3 08-16-2005 04:40 PM
nawk not available raees Linux - Software 3 05-02-2005 04:49 PM
Nawk issues michedlp Programming 1 02-25-2004 08:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration