[!NOTE|label:references:]

sum

[!INFO|label:references:]

awk

$ seq 10 | awk '{s+=$1} END {print s}'
55

# or
$ awk 'BEGIN{print '"1+2+3"'}'
6

file sizes

$ ls -l
total 12
-rw-r--r--  1 marslo staff 3480 Dec 21 21:21 README.md
-rw-r--r--  1 marslo staff 7516 Dec 21 21:21 SUMMARY.md
drwxr-xr-x  7 marslo staff  224 Sep 19 19:52 artifactory
drwxr-xr-x 15 marslo staff  480 Dec 21 21:19 cheatsheet
drwxr-xr-x 12 marslo staff  384 Aug 17 21:11 devops
drwxr-xr-x  9 marslo staff  288 Sep 29 17:31 jenkins
drwxr-xr-x 10 marslo staff  320 Sep 19 19:52 linux
drwxr-xr-x  9 marslo staff  288 Sep 19 19:52 osx
drwxr-xr-x  6 marslo staff  192 Aug 17 21:11 programming
drwxr-xr-x 27 marslo staff  864 Aug 17 22:10 screenshot
drwxr-xr-x  7 marslo staff  224 Oct 11 19:41 tools
drwxr-xr-x  8 marslo staff  256 Aug 30 16:39 vim
drwxr-xr-x  5 marslo staff  160 Aug 17 21:11 virtualization

$ ls -l | awk '{sum += $5} END {print sum}'
14676

datamash

[!TIP|label:reference:]

$ seq 10 | datamash sum 1
55

bc

  • paste + bc

    $ seq 10 | paste -sd+ -
    1+2+3+4+5+6+7+8+9+10
    
    $ seq 10 | paste -sd+ - | bc
    55
    
  • xargs + bc

    $ seq 10 | xargs printf "- - %s" | xargs | bc
    55
    
  • sed + bc

    $ seq 10 | sed 's/^/.+/' | bc
    1
    3
    6
    10
    15
    21
    28
    36
    45
    55
    

jq

$ seq 10 | jq -s 'add'
55

$(())

$ f=$(seq 10)
$ echo $(( ${f//$'\n'/+} ))
55

# or
$ echo $(( $(seq 10 | tr "\n" "+") 0 ))
55
# or from file
$ echo $(( $( tr "\n" "+"  < /tmp/test) 0 ))

sum from file

[!NOTE|label:sample file:]

$ cat numbers.txt
73.27
218.38
14.15
9.18
16.60
  • awk

    
    $ awk '{ sum += $1 } END { print sum }' numbers.txt
    331.58
    
  • paste && bc

    $ paste -sd+ numbers.txt
    73.27+218.38+14.15+9.18+16.60
    
    $ paste -sd+ numbers.txt | bc
    331.58
    
    • Σn where 1<=n<=100000
      $ seq 100000 | paste -sd+ | bc -l
      5000050000
      
  • jq

    $ paste -sd' ' numbers.txt | jq -s add
    331.58
    

advanced computing

[!NOTE|label:references:]

logarithm

  • bc

    $ bc -l <<< 'l(9)/l(3)'
    2.00000000000000000000
    
    • 🫠
      $ bc -l <<< 'l(512)/l(2)'
      9.00000000000000000008
      
  • awk

    $ echo 512 | awk '{print log($1)/log(2)}'
    9
    

power

  • bc

    $ bc <<< '2^3'
    8
    
  • $(())

    $ echo $(( 2**8 ))
    256
    

square

$ bc -l <<< 'sqrt(2)'
1.41421356237309504880

$ bc <<< 'sqrt(2)'
1

decimal to binary

[!NOTE|label:references:]

  • bc

    $ bc <<< 'obase=2;15'
    1111
    
    $ bc -l <<< 'obase=2;0;0;15;255' | xargs
    0 0 1111 11111111
    
    $ bc -l <<< 'obase=2;0;0;15;255' | awk '{ printf "%08d\n", $0 }' | xargs
    00000000 00000000 00001111 11111111
    
    $ printf "%08d\n" $(echo "obase=2; 0;0;15;255" | bc) | xargs
    00000000 00000000 00001111 11111111
    
    $ bc <<< 'obase=2; 0;0;15;255' | numfmt --format=%08f | xargs
    00000000 00000000 00001111 11111111
    

binary <> decimal <> hexadecimal

[!NOTE]

  • obase : [o]utput base
  • ibase : [i]utput base
# bin -> dec
$ bc <<< 'ibase=2;11111111;11111111;11000000;00000000' | paste -sd. -
255.255.192.0

# bin -> hex
$ bc <<< 'obase=16;ibase=2;11111111;11111111;11000000;00000000' | awk '{ printf "%04s\n", $1 }' | paste -sd. -
00FF.00FF.00C0.0000

# dec -> bin
$ bc <<< 'ibase=10;obase=2;255;255;240;0' | numfmt --format %08f | paste -sd' ' -
11111111 11111111 11110000 00000000

# dec -> hex
$ bc <<< 'ibase=10;obase=16;255;255;240;0' | awk '{ printf "%04s\n", $1 }' | paste -sd. -
00FF.00FF.00F0.0000

# hex -> bin
$ bc <<< 'ibase=16;obase=2;FF;FF;EE;0A' | numfmt --format %08f | paste -sd' ' -
11111111 11111111 11101110 00001010

# hex -> dec
$ bc <<< 'ibase=16;FF;FF;EE;0A' | paste -sd. -
255.255.238.10
Copyright © marslo 2020-2023 all right reserved,powered by GitbookLast Modified: 2024-03-12 15:01:28

results matching ""

    No results matching ""