Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: [bash] How to add a lot of numbers?

  1. #1
    Join Date
    Jul 2007
    Location
    India
    Beans
    147
    Distro
    Ubuntu

    [bash] How to add a lot of numbers?

    I'm having a problem adding numbers from a file and displaying it on the screen.

    The file is like this,

    121
    213
    2354
    23
    5878
    546
    65

    Now, I want to add up these numbers and display.
    I "dont" want to use "cat file | while read var" loop because it is slow and the variable created inside it isn't accessible from outside the loop.

    Please suggest me ways.

    Also, if you'd help me, suggest me the fastest way to read a file full of file paths (newline separated) and calculate their total file size, I've used command " cat file | xargs du " but it says the list of arguments is too large.

    Thanks.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [bash] How to add a lot of numbers?

    <edit>
    Never mind, shell solution given.
    </edit>

  3. #3
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: [bash] How to add a lot of numbers?

    Quote Originally Posted by mridkash View Post
    Now, I want to add up these numbers and display.
    I "dont" want to use "cat file | while read var" loop because it is slow and the variable created inside it isn't accessible from outside the loop.
    what about
    Code:
    martin@toshibap200:~$ cat test.txt
    121
    213
    2354
    23
    5878
    546
    65
    martin@toshibap200:~$ cat script.bash 
    #!/bin/sh
    value=0
    while read var
    do
    value=`expr $value + $var`
    done < test.txt
    echo $value
    martin@toshibap200:~$ 
    martin@toshibap200:~$ ./script.bash 
    9200

  4. #4
    Join Date
    Oct 2007
    Location
    Vienna, Europe
    Beans
    77

    Re: [bash] How to add a lot of numbers?

    Quote Originally Posted by mridkash View Post
    I've used command " cat file | xargs du " but it says the list of arguments is too large.
    This is a good discussion of the problem, including some possible solutions:

    http://www.linuxjournal.com/article/6060
    Last edited by a9bejo; February 25th, 2008 at 06:51 PM.

  5. #5
    Join Date
    Nov 2004
    Location
    Portland, Or, USA
    Beans
    289
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [bash] How to add a lot of numbers?

    Two completely different questions...

    The script that you've already been given will work, and seems reasonable enough...

    Given that you have that you could always read your second file of path names and add the results of du <name> to a second file and then run that through your first script...

    Alternatively you could try...

    Code:
    #! /bin/sh
    
    file=$1 # Add some error checking/usage stuff around this
    
    total=0
    
    for path in `cat ${file}`
    do
      pathsize=`du -s ${path} | cut -f 1`
      total=`expr ${total} + ${pathsize}`
    done
    
    echo $total
    
    exit 0
    Presumably you want to get the total sizes of the directories including their subdirectories... du <dir> lists the sizes of each of the files in that directory...

    I hope that this helps,

    Z.

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [bash] How to add a lot of numbers?

    Quote Originally Posted by mridkash View Post
    I'm having a problem adding numbers from a file and displaying it on the screen.

    The file is like this,

    121
    213
    2354
    23
    5878
    546
    65

    Now, I want to add up these numbers and display.
    I "dont" want to use "cat file | while read var" loop because it is slow and the variable created inside it isn't accessible from outside the loop.
    Code:
    awk '{total+=$0}END{print total}' file
    Also, if you'd help me, suggest me the fastest way to read a file full of file paths (newline separated) and calculate their total file size, I've used command " cat file | xargs du " but it says the list of arguments is too large.
    Thanks.
    Code:
    while read -r file
    do 
      total=`du -s "$file"`
      echo "Total size for $file is : $total"
    done < "input"

  7. #7
    Join Date
    Jul 2007
    Location
    India
    Beans
    147
    Distro
    Ubuntu

    Re: [bash] How to add a lot of numbers?

    Thanks guys.
    But one thing I dont understand is, why the "for" or "while" loop runs so slowly in bash.

    Example, I was doing,
    Code:
    cat path/to/file | while read files ; do
    echo -n $files | grep -v -z -e "[/][.]"  >> $backupList
    done
    And then I tried,
    Code:
    grep -v -e "[/][.]" path/to/file > $backupList
    And to my surprize, the second one finished in a jiffy and the first one took like 5-10 seconds.
    How?

  8. #8
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [bash] How to add a lot of numbers?

    Quote Originally Posted by mridkash View Post
    Thanks guys.
    But one thing I dont understand is, why the "for" or "while" loop runs so slowly in bash.

    Example, I was doing,
    Code:
    cat path/to/file | while read files ; do
    echo -n $files | grep -v -z -e "[/][.]"  >> $backupList
    done
    cat is not needed, if you are using the while loop. Try not to use cat unnecessarily as possible. grep's "looping" mechanism is compiled into the tool itself ( C language). Whereas you have to explicitly call while loop in bash. Imagine what happens if you could do this in bash
    Code:
    # bash -v -e "pattern" file
    And then I tried,
    Code:
    grep -v -e "[/][.]" path/to/file > $backupList
    And to my surprize, the second one finished in a jiffy and the first one took like 5-10 seconds.
    How?
    as explained above.

  9. #9
    Join Date
    Nov 2004
    Location
    Portland, Or, USA
    Beans
    289
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [bash] How to add a lot of numbers?

    Every time a shell calls a program it has to instantiate that program (fork exec) run it and get the results... If you are only using built-ins it's much faster or if you cut the number of programs you need to run it speeds up.

    Your first example uses

    one program outside the loop but inside the loop does...

    read, echo, grep

    for every file... Even if the grep was the only one that is called thats still a lot more than
    the second version which runs one command.

    Z.

  10. #10

    Re: [bash] How to add a lot of numbers?

    One line version of the script above (for the impatient).

    Code:
    value=0; while read var; do value=`expr $value + $var`; done < test.txt; echo $value

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •