Sponsored Content
Full Discussion: How to sum these value.
Top Forums Shell Programming and Scripting How to sum these value. Post 302590034 by Corona688 on Friday 13th of January 2012 02:17:09 PM
Old 01-13-2012
Quote:
Originally Posted by ken002
another thing, Would you please explain little about "bc", what this one doing, I can not find it in google.
Code:
$ man bc

bc(1)                                                                    bc(1)



NAME
       bc - An arbitrary precision calculator language

SYNTAX
       bc [ -hlwsqv ] [long-options] [  file ... ]

DESCRIPTION
       bc  is a language that supports arbitrary precision numbers with inter-
       active execution of statements.  There are  some  similarities  in  the
       syntax  to  the  C  programming  language.   A standard math library is
       available by command line option.  If requested, the  math  library  is
       defined before processing any files.  bc starts by processing code from
       all the files listed on the command line in the  order  listed.   After
       all  files  have been processed, bc reads from the standard input.  All
       code is executed as it is read.  (If a file contains a command to  halt
       the processor, bc will never read from the standard input.)

...

$ echo "1+2" | bc
3
$

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

sum of more than 1 column

Hi I want to sum of 3 columns in file. Example: I want to sum of 3 ,6,8 th columns in file(SUM(3,6,8)). Using awk can sum of single column awk '{a+=$3} END {printf ("%f\n",a)' file_name Thanks inadvance MR (2 Replies)
Discussion started by: mohan705
2 Replies

2. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies

3. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

4. Solaris

How to Sum

Hi I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below 1 23 1,456 1 1 34 46 How do I calculate the sum of all these numbers to ouptut the answer ( 1,562) Thanks in advance (3 Replies)
Discussion started by: rob171171
3 Replies

5. UNIX for Dummies Questions & Answers

Getting the sum

I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still... (7 Replies)
Discussion started by: cosmologist
7 Replies

6. Shell Programming and Scripting

Format the value of sum

I have a list of values ( in Kb) I have the following code to sum up the values and convert the total to GB cat list 701368101370 101370101370 801554101370 701636101370 101757101370 101876101370 901951101370 And this is the output of my script awk '{ s += $1 } END {... (3 Replies)
Discussion started by: Sara_84
3 Replies

7. Shell Programming and Scripting

Sum of all columns

Hi Friends, I have a file with fields separated with comma. How to print sum of each field of the file? Eg: input file 1,3,6,7 2,1,2,1 0,1,1,0 I want to sum each field separately. Output file 3,5,9,8 Thanks, Suresh (2 Replies)
Discussion started by: suresh3566
2 Replies

8. UNIX for Dummies Questions & Answers

Sum with condition

Dear masters, I have file input 011171646073|12129|12129|A027|20141001|20141015|2014|10|01|2013|10|15 011171646076|12129|12129|A027|20141001|20141012|2014|10|01|2014|09|12 011171646078|12129|12129|A027|20141001|20141015|2014|10|01|2014|10|15... (6 Replies)
Discussion started by: radius
6 Replies

9. Shell Programming and Scripting

How to get the sum?

hi Gurus, I have an extract as seen below: INPUT 2015-08-24 15:00:00.0 |TCSERVER01 |ServiceEventHandler |2283 2015-08-24 15:01:00.0 |TCSERVER01 |ServiceEventHandler |576 2015-08-24 15:02:00.0 |TCSERVER01 |ServiceEventHandler |833 2015-08-24 15:03:00.0 |TCSERVER01 |ServiceEventHandler... (6 Replies)
Discussion started by: reignangel2003
6 Replies

10. Shell Programming and Scripting

Sum of Columns

HI Guys, I gave Input file F.Txt ID H1 H2 H3 H4 H5 A 5 6 7 8 9 B 4 65 4 4 7 C 4 4 4 4 4 D 4 4 4 4 4 Output :- ID H1 H2 H3 H4 H5 Total 17 79 19 20 24 Sum of Each Columns (8 Replies)
Discussion started by: pareshkp
8 Replies
BC(1)							    BSD General Commands Manual 						     BC(1)

NAME
bc -- arbitrary-precision arithmetic language and calculator SYNOPSIS
bc [-chlv] [-e expression] [file ...] DESCRIPTION
bc is an interactive processor for a language which resembles C but provides unlimited precision arithmetic. It takes input from any expres- sions on the command line and any files given, then reads the standard input. Options available: -c bc is actually a preprocessor for dc(1), which it invokes automatically, unless the -c (compile only) option is present. In this case the generated dc(1) instructions are sent to the standard output, instead of being interpreted by a running dc(1) process. -e expression, --expression expression Evaluate expression. If multiple -e options are specified, they are processed in the order given, separated by newlines. -h, --help Prints usage information. -l, --mathlib Allow specification of an arbitrary precision math library. The definitions in the library are available to command line expres- sions. -v, --version Prints version information. The syntax for bc programs is as follows: 'L' means letter a-z; 'E' means expression; 'S' means statement. As a non-portable extension, it is possible to use long names in addition to single letter names. A long name is a sequence starting with a lowercase letter followed by any number of lowercase letters and digits. The underscore character ('_') counts as a letter. Comments are enclosed in /* and */ are enclosed in # and the next newline The newline is not part of the line comment, which in itself is a non-portable extension. Names simple variables: L array elements: L [ E ] The words `ibase', `obase', and `scale' The word `last' or a single dot Other operands arbitrarily long numbers with optional sign and decimal point ( E ) sqrt ( E ) length ( E ) number of significant decimal digits scale ( E ) number of digits right of decimal point L ( E , ... , E ) The sequence '<newline><whitespace>' is ignored within numbers. Operators The following arithmetic and logical operators can be used. The semantics of the operators is the same as in the C language. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. Operator Associativity Description ++ -- none increment, decrement - none unary minus ^ right power * / % left multiply, divide, modulus + - left plus, minus = += -= *= /= %= ^= right assignment == <= >= != < > none relational ! none boolean not && left boolean and || left boolean or Note the following: o The relational operators may appear in any expression. The IEEE Std 1003.1-2008 (``POSIX.1'') standard only allows them in the conditional expression of an 'if', 'while' or 'for' statement. o The relational operators have a lower precedence than the assignment operators. This has the consequence that the expression a = b < c is interpreted as (a = b) < c, which is probably not what the programmer intended. o In contrast with the C language, the relational operators all have the same precedence, and are non-associative. The expression a < b < c will produce a syntax error. o The boolean operators (!, && and ||) are non-portable extensions. o The boolean not (!) operator has much lower precedence than the same operator in the C language. This has the consequence that the expression !a < b is interpreted as !(a < b). Prudent programmers use parentheses when writing expressions involving boolean oper- ators. Statements E { S ; ... ; S } if ( E ) S if ( E ) S else S while ( E ) S for ( E ; E ; E ) S null statement break continue quit a string of characters, enclosed in double quotes print E ,..., E A string may contain any character, except double quote. The if statement with an else branch is a non-portable extension. All three E's in a for statement may be empty. This is a non-portable extension. The continue and print statements are also non-portable extensions. The print statement takes a list of comma-separated expressions. Each expression in the list is evaluated and the computed value is printed and assigned to the variable `last'. No trailing newline is printed. The expression may also be a string enclosed in double quotes. Within these strings the following escape sequences may be used: 'a' for bell (alert), '' for backspace, 'f' for formfeed, ' ' for newline, ' ' for carriage return, ' ' for tab, 'q' for double quote and '\' for backslash. Any other character following a backslash will be ignored. Strings will not be assigned to `last'. Function definitions define L ( L ,..., L ) { auto L, ... , L S; ... S return ( E ) } As a non-portable extension, the opening brace of the define statement may appear on the next line. The return statement may also appear in the following forms: return return () return E The first two are equivalent to the statement ``return 0''. The last form is a non-portable extension. Not specifying a return statement is equivalent to writing ``return (0)''. Functions available in the math library, which is loaded by specifying the -l flag on the command line s(x) sine c(x) cosine e(x) exponential l(x) log a(x) arctangent j(n,x) Bessel function All function arguments are passed by value. The value of a statement that is an expression is printed unless the main operator is an assignment. The value printed is assigned to the special variable `last'. This is a non-portable extension. A single dot may be used as a synonym for `last'. Either semicolons or newlines may separate statements. Assignment to scale influences the number of digits to be retained on arithmetic operations in the manner of dc(1). Assignments to ibase or obase set the input and output number radix respectively. The same letter may be used as an array, a function, and a simple variable simultaneously. All variables are global to the program. `Auto' variables are pushed down during function calls. When using arrays as function arguments or defining them as automatic variables, empty square brackets must follow the array name. For example scale = 20 define e(x){ auto a, b, c, i, s a = 1 b = 1 s = 1 for(i=1; 1==1; i++){ a = a*x b = b*i c = a/b if(c == 0) return(s) s = s+c } } defines a function to compute an approximate value of the exponential function and for(i=1; i<=10; i++) e(i) prints approximate values of the exponential function of the first ten integers. $ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit prints an approximation of pi. COMMAND LINE EDITING
bc supports interactive command line editing, via the editline(3) library. It is enabled by default if input is from a tty. Previous lines can be recalled and edited with the arrow keys, and other GNU Emacs-style editing keys may be used as well. The editline(3) library is configured with a .editrc file - refer to editrc(5) for more information. FILES
/usr/share/misc/bc.library math library, read when the -l option is specified on the command line. COMPATIBILITY
The -q and --quiet options are no-ops for compatibility with some other implementations of bc and their use is discouraged. SEE ALSO
dc(1) STANDARDS
The bc utility is compliant with the IEEE Std 1003.1-2008 (``POSIX.1'') specification. The flags [-ce], as well as the parts noted above, are extensions to that specification. HISTORY
The bc command first appeared in Version 6 AT&T UNIX. A complete rewrite of the bc command first appeared in OpenBSD 3.5. AUTHORS
The original version of the bc command was written by Robert Morris and Lorinda Cherry. The current version of the bc utility was written by Otto Moerbeek. BUGS
'Quit' is interpreted when read, not when executed. Some non-portable extensions, as found in the GNU version of the bc utility are not implemented (yet). BSD
April 16, 2014 BSD
All times are GMT -4. The time now is 05:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy