Help to understand the command

Hi Gurus,

I am new for Unix scripting. below command in one existing script. I am not able to fully understand. please help

in below command, I am not able to understand what's {P=1} do

thanks in advance

 
 awk 'NF==1{$3=$1;$1=L}P&&NF>=3{print $1,$3;L=$1}/^___/{P=1}' FILE
 

Hello green_k,

Welcome to UNIX and Linux forums, hope you will enjoy sharing and learning knowledge here. Following explanation may help you in your question.

awk 'NF==1{              ##checking condition here if NF(number of fields on current line) is 1 then do following.
            $3=$1;       ##Setting 3rd field value same as 1st field value.
            $1=L}        ##Setting 1st field value as the value of variable L here.
      P&&NF>=3{          ##checking condition here if variable P is NOT NULL and NF is greater OR equal to 3 then do following.
            print $1,$3; ##Printing 1st and 3rd fields values here.
            L=$1}        ##Setting variable L value as $1 of current line.
      /^___/{            ##checking condition here if a line starts with ___ then do following.
            P=1}         ##Setting variable P value to 1 here.
' FILE                   ##Mentioning Input_file name here.
 

Enjoy learning and keep sharing knowledge :b:

Thanks,
R. Singh

3 Likes

Thanks, RavinderSingh13, for this explanation. Small, nit-picking side remark: Nothing can be "greater than" AND "equal to" something, that's mutually exclusive. "Greater than or equal" would perfectly describe that test's operation.

@green_k: P is used as a logical or boolean variable here. A zero value (or unset) represents a boolean FALSE value, any other, esp. 1, represents TRUE. You can see that when P itself is used as a (sub-) pattern, without an operation like a test done on it. P && ... reads like if (P is TRUE) and ... .

2 Likes

thanks RavinderSingh13 and RudiC for your detail explanation. it is clear like crystal.