Help to understand the command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to understand the command
# 1  
Old 04-07-2018
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

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

# 2  
Old 04-07-2018
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.
Code:
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 Smilie

Thanks,
R. Singh

Last edited by RavinderSingh13; 04-07-2018 at 12:43 PM.. Reason: Thanks Rudi sir for letting me know the typo here.
These 3 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-07-2018
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 ....

Last edited by RudiC; 04-07-2018 at 12:46 PM..
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 04-07-2018
thanks RavinderSingh13 and RudiC for your detail explanation. it is clear like crystal.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help understand awk command

Help understand awk command This command converts the column values to rows. Command: awk -s1=" " '{S=S?S OFS s1 $0 s1:s1 $0 s1} END{print S}' OFS=, Input_file Example: 1 2 3 is converted to: 1, 2, 3 Can anyone please help me understand this command? Please use code tags when... (1 Reply)
Discussion started by: mohan44
1 Replies

2. UNIX for Beginners Questions & Answers

I can not understand the command from the systemd?

journalctl --since "tomorrow" By idea to show magazines from tomorrow. As it is illogical. Tell me what is the essence of the team with the key tomorrow? Code tags please (1 Reply)
Discussion started by: alekseev
1 Replies

3. Shell Programming and Scripting

Not able to understand use exec command

Hi i am in learning phase of unix. i was going through exec in a unix book. below is the command exec n>file exec n>>file however when i used the exec command like below , where ex is the file name exec 2>>exand then do ls -lrt then again when i do the ls -lrt to see the size of the file... (3 Replies)
Discussion started by: scriptor
3 Replies

4. Solaris

Looking to understand what this command $$ does in variable assignment?

Hi Folks, I'm looking to figure something out in an existing script I'm trying to understand. the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM Now, I know it's setting the $workdir environmental variable... And I understand most of... (2 Replies)
Discussion started by: Marc G
2 Replies

5. UNIX for Dummies Questions & Answers

Not able to understand the output of w command

I have taken putty session of a server from two separate machines namely HOST1(3 sessions) and HOST2(1 Session) . However w command says there are 5 users Confused over the output any clue will be appreciated. # w 09:29:36 up 34 days, 15:48, 5 users, load average: 0.62, 4.33, 8.16 USER ... (3 Replies)
Discussion started by: pinga123
3 Replies

6. Shell Programming and Scripting

Need to understand the output of last command

root@desktop:~# last reboot | head -1 reboot system boot 2.6.31-17-generi Tue Jan 26 12:05 - 13:52 (01:46) What does the last two fields(12:05 - 13:52 (01:46)) of the output mean? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

7. Shell Programming and Scripting

Sed command - help me understand

Hello, can someone please explain me what the following commands do.. i know the output but i would like to understand the break down of what they do step by step. 1) sed -n "/ $(date +\%R -d "-1 min")/,$"p req.txt| wc -l 2) awk '/19:00/,/22:00/' app.log |grep "mytesturl"|grep... (2 Replies)
Discussion started by: niks
2 Replies

8. HP-UX

Need help to understand chatr command

Hi, I am trying to find the max memory utilization of a binary file using chatr command. i gave chatr <file name>. But no where i could see the max memory usage of the file. How do you i check the memory usage of this binary file. Upto my knowledge it should be 1.75GB in HP-UX. But the does... (1 Reply)
Discussion started by: kprasanna_79
1 Replies

9. UNIX for Dummies Questions & Answers

Just a quickie to help me understand this command:

Hi. Im working in AIX. Oracle 9.2. In one of the backup scripts, I see this: sysback -f'/dev/rmt0' '-T chrp' '-k mp' -w '/tmpback' 'oradata_vg oraindx_vg ora misc_vg' The line here, as I know, specifies the device name, the temp file, and the volume groups. My query regards theses two: '-T... (2 Replies)
Discussion started by: Incremental
2 Replies

10. UNIX for Dummies Questions & Answers

Can't understand sar command

HI Experts, Can anyone pls help me to understand this.. >sar 20:05:00 1 2 1 96 20:10:00 2 2 10 87 20:15:00 1 2 19 78 20:20:00 1 2 14 83 20:25:00 1 2 16 81 20:30:00 1 ... (1 Reply)
Discussion started by: shaan_dmp
1 Replies
Login or Register to Ask a Question