Explaination on Behavior of awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explaination on Behavior of awk command
# 1  
Old 08-01-2013
Explaination on Behavior of awk command

Hello Admin,
Could you pls explain on the below behavior of the awk command.

Code:
$ awk -F">20" "/Cyclomatic complexity/ && /;add;/{print \$1}" inspect_64d_369980 | awk '{print $NF}' | sort | tail -1
65


Code:
$var=`awk -F">20" "/Cyclomatic complexity/ && /;add;/{print \$1}" inspect_64d_369980 | awk '{print $NF}' | sort | tail -1`;echo $var
65>20;;Existing;;Analyze;21190;htgoto:project=F_64_d,pid=21190

Moderator's Comments:
Mod Comment Using Bold font isn't helping here. Please use CODE tags to show sample code, input, and output.



Regards,
Chandana

Last edited by Don Cragun; 08-01-2013 at 06:49 AM.. Reason: Add CODE tags
# 2  
Old 08-01-2013
Can you post the file inspect_64d_36998
You should also change " to ' in awk
awk -F">20" '/Cyclomatic complexity/ && /;add;/{print \$1}' inspect_64d_369980

And its better to use $() instead of backtics ``

Code:
var=$(awk -F">20" '/Cyclomatic complexity/ && /;add;/{print \$1}' inspect_64d_369980 | awk '{print $NF}' | sort | tail -1);echo $var

This User Gave Thanks to Jotne For This Post:
# 3  
Old 08-01-2013
Again, if you don't show us your input file, we can't guess at what might be present in the input file that is making you think this code isn't working. Which awk command is confusing you?

The 1st awk command selects every line in the file named inspect_64d_369980 that contains both of the strings Cyclomatic complexity and ;add;. Then for each selected line, it prints that entire line if the string >20 does not appear on the line; otherwise it prints the contents of the line before the 1st occurrence of the string >20.

The second awk then prints the string following the last space or tab on the lines printed by the 1st awk, or (if there were no space or tab characters) the entire line.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-05-2013
Hello,
Thanks, the solution provided worked...

Sorry for the missing input file.

Code:
 $ grep -e "Cyclomatic complexity" -e ";add;" inspect_64d_369980 | head -5
  Thread.cpp;176;25;Error;;3;NPD.FUNC.MUST;Error;add;-73966069;Pointer 'handle.operator->()' returned from call to function 'operator->' at line 176 may be NULL and will be dereferenced at line 176.;;Existing;;Analyze;2254;https://insight-review.html#goto:project= 64_d,pid=2254
  Bond.cxx;268;8;Error;;3;MLK.MIGHT;Error;add;-1408122275;Possible memory leak. Dynamic memory stored in 'bond' allocated through function 'new' at line 249 can be lost at line 268;;Existing;;Analyze;3998;https://insight-review.html#goto:project=64_d,pid=3998
  DescHolder.hpp;168;41;Error;;3;NPD.FUNC.MUST;Error;add;-116936433;Pointer 'conn.operator->()' returned from call to function 'operator->' at line 168 may be NULL and will be dereferenced at line 168.;;Existing;;Analyze;13987;https://insight-review.html#goto:project=64_d,pid=13987
  Command.java;62;22;Style;;8;JD.VNU;Warning;add;-934426595;Variable 'result' was never read after being assigned.;;Existing;;Analyze;16949;https://insight-review.html#goto:project=64_d,pid=16949
  FilterCommand.java;63;28;Style;;8;JD.VNU;Warning;add;104541;Variable 'irf' was never read after being assigned.;;Existing;;Analyze;16950;https://insight-review.html#goto:project=64_d,pid=16950




Output (worked
after changing from " to ')
Code:
 $ awk -F">20" "/Cyclomatic complexity/ && /;add;/{print \$1}" inspect_64d_369980 | awk '{print $NF}' | sort | tail -1
  65
  $ awk -F">20" '/Cyclomatic complexity/ && /;add;/{print $1}' inspect_64d_369980 | awk '{print $NF}' | sort | tail -1
  65
  $ var=`awk -F">20" "/Cyclomatic complexity/ && /;add;/{print \$1}" inspect_64d_369980 | awk '{print $NF}' | sort | tail -1`;echo $var
  65>20;;Existing;;Analyze;21190;https://insight-review.html#goto:project=64_d,pid=21190
  $var=`awk -F">20" '/Cyclomatic complexity/ && /;add;/{print $1}' inspect_64d_369980 | awk '{print $NF}' | sort | tail -1`;echo $var 
  65
  $ var=$(awk -F">20" '/Cyclomatic complexity/ && /;add;/{print $1}' inspect_64d_369980 | awk '{print $NF}' | sort | tail -1);echo $var
65

Thanks for the replies and solutionSmilie

Last edited by Don Cragun; 08-05-2013 at 03:19 AM.. Reason: CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Strange behavior from kill command

I am getting some strange behaviour from the kill command. When I run the which command it says it points to /usr/bin/kill. When I look at my PATH I have /usr/bin in it. So why does running kill or /usr/bin/kill produce different outputs? ghost ~ $ which kill /usr/bin/kill ghost ~ $ kill... (5 Replies)
Discussion started by: cokedude
5 Replies

2. UNIX for Beginners Questions & Answers

awk Behavior

Linux Release Uname details Data file Ive been at the command line for some time. Back as far as SCO and Interactive Unix. I have always used this construct without issues. I want to isolate the ip / field 1. As you can see .. the first line is "skipped". This works as... (6 Replies)
Discussion started by: sumguy
6 Replies

3. Shell Programming and Scripting

Strange behavior of find and rm command

Hi I run the below command to find and delete *.xml files 90 or more days old. find . -type f -name '*.xml' -mtime +90 -exec rm {} \; find: stat() error ./Hello/2014_EMPTY.xml: No such file or directory ./Hello/2014_EMPTY_8011.xml: No such file or directory ..... .... If the file... (10 Replies)
Discussion started by: mohtashims
10 Replies

4. Shell Programming and Scripting

Explaination on export command

Hello Team, Could you pls explain how export command works in below code: for i in ${!SDV_*}; do export $i done As per my understanding, if SDV_1=test1;SDV_2=test2;test1=var1;test2=var2then in for loop below export will get executed. export var1;export var2But, Will this... (3 Replies)
Discussion started by: chandana.hs
3 Replies

5. UNIX for Advanced & Expert Users

Behavior of Find command

Centos 5.8 Lets say I have 2 nfs shares mounted to /folder1 and /folder2. If I do a find / -name *something* Will it also search the 2 nfs shares ? If so is there a way to avoid this? Thanks (2 Replies)
Discussion started by: whegra
2 Replies

6. Red Hat

BASH command not found strang behavior

Hi all I am relatively new to linux (specifically red hat). I have installed Fedora 13 on my machine and started playing with the terminal when i found a very strange behavior when typing a command that is not found: the terminal does not prompt me back. In other words, i am logged as root (or... (4 Replies)
Discussion started by: abohmeed
4 Replies

7. Shell Programming and Scripting

awk print behavior weird

Hi Experts I am facing a weird issue while using print statement in awk. I have a text file with 3 fields shown below: # cat f1 234,abc,1000 235,efg,2000 236,jih,3000 # When I print the third column alone, I dont face any issue as shown below: # awk '{print $3 }' FS=, f1 1000 2000... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

8. Shell Programming and Scripting

Looking for awk code explaination

{ # print NF,NR,$0; if ( ($(NF-1) != 0) && ($NF != 0) ) {if ($(NF-1) > $NF) {percent=$(NF-1)/$NF-1;} else {percent=$NF/$(NF-1)-1;} } printf "%8.4f\%\n",percent*100; if (percent > 0.05||percent < -0.05 ){exit 1;} }' Use code tags please, ty. Also try to use a more... (1 Reply)
Discussion started by: bosmat shani
1 Replies

9. Linux

I would like to know the explaination.

Hi, I'm new to LINUX Scripting, I would like to know the full explaination of the below scripts. thank you. 1st script #! /bin/sh . /opt/home/hssadmin/cindy/formatxml.env `testrecord.scp` `testEXGU.scp` 2nd Script #! /bin/sh . /opt/home/hssadmin/cindy/formatxml.env cd... (1 Reply)
Discussion started by: AudreyEliza
1 Replies
Login or Register to Ask a Question