Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

bash conditional sum

# Basic syntax:
awk '{ if ( condition ) { sum += $field; }} END { print sum }' your_file
# Where:
#	- $field is the field to sum if the condition is true

# Example usage:
# Sum all fields of column 5 in your_file that don't have "*" in field 3
awk '{ if ($3 != "*" ) { sum += $5; }} END { print sum }' your_file
# Note, explicitly setting your_file's field delimiter with 
#	"BEGIN { FS="<input_delimiter>" };" might be needed for non-standard 
#	delimiters
Source by www.unix.com #
 
PREVIOUS NEXT
Tagged: #bash #conditional #sum
ADD COMMENT
Topic
Name
9+9 =