Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > why do this command's output is zero?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 19th October 07:03
elvistudio
External User
 
Posts: 1
Default why do this command's output is zero?



Hello!
Could you tell me why do this command's output is zero ("0")? Many
lines of file 'trace' contain the string "Exception"...

awk 'BEGIN{i=""}$0 ~ /Exception/{i+=$0}END{print i}' trace (or awk
'BEGIN{i=""}{if ($0 ~ /Exception/) i+=$0}END{print i}' trace)

Thanks!
  Reply With Quote


 


2 19th October 07:03
vassilis
External User
 
Posts: 1
Default why do this command's output is zero?



Try this out:

awk 'BEGIN{i=""}
$0 ~ /Exception/ { printf "%s = %.6g\n", $0, $0; i += $0 }
END {print i}' trace

What do you think?
  Reply With Quote
3 19th October 07:03
ed morton
External User
 
Posts: 1
Default why do this command's output is zero?


+= is for arithmetic addition, not string concatentation.

Ed.
  Reply With Quote
4 19th October 07:03
chris f.a. johnson
External User
 
Posts: 1
Default why do this command's output is zero?


What are you trying to do?

If you want to count the number of lines containing Exception:

awk '/Exception/ { ++i } END { print i }' trace

If you want to concatenate all lines containing Exception:

awk '/Exception/ { i = i $0 } END { print i }' trace

Or, if you want to preserve the lines:

awk '/Exception/ { i = i $0 "\n" } END { printf "%s", i }' trace --
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
  Reply With Quote
Reply


Thread Tools
Display Modes




666