Shell script to select all numbers from a file

user3002428

I have a file that contains words and numbers at random positions. I must write a shell script that adds all the numbers from the file.

I thought about something like this:

filename=$1
words=` echo $filename `
sum=0
for word in words;
do
    #if $word is number, add to sum
done

How can I verify if something is a number in shell? Or this whole approach is wrong?

fedorqui 'SO stop harming'

You can for exapmle use this:

grep -ow '[0-9]*' file | awk '{count+=$1} END{print count}'

Explanation

  • grep -ow '[0-9]*' file gets all the blocks of numbers of the file. -w matches just words, so that a23b won't be a match.
  • awk '{count+=$1} END{print count}' loops through the grep result adding each one of the numbers to the count variable. Finally, prints the result.

Example

$ cat a
aa b23 23 55
adf 23 23 62l

$ grep -ow '[0-9]*' a | awk '{count+=$1} END{print count}'
124

$ grep -ow '[0-9]*' a
23
55
23
23

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

dividing all numbers in a file using shell scripting

From Dev

converting variables in a shell script from strings to numbers

From Dev

Adding sequential numbers to the end of file names - Shell Script

From Dev

Why is my shell script putting arbitrary numbers in this file?

From Dev

Proces a text file shell script that have numbers and strings

From Dev

Compare the numbers in text file, if meets the condition run a shell script

From Dev

shell script to extract specific columns of a line based on numbers of a file

From Dev

export binary file from shell script to browser

From Dev

Create , Write and Save file from a Shell script

From Dev

Cannot execute a shell script from gradle file

From Dev

Reading data from properties file in shell script

From Dev

extracting a row from a file using shell script

From Dev

linux shell script read from stdin if no file

From Dev

Pass variable from shell script to config file

From Dev

How to register log file from shell script

From Dev

Create , Write and Save file from a Shell script

From Dev

Reading data from properties file in shell script

From Dev

Extract filename from file using shell script

From Dev

Script Shell: Delete a line from a file

From Dev

Extract line from a file in shell script

From Dev

Writing commands to a file from a shell script

From Dev

Extract values from file using shell script

From Dev

Shell script read separately columns from file

From Dev

Read all the numbers from line of file

From Dev

Help with script reading numbers from a file and determining if they are even or odd numbers

From Dev

Place all sed commands into one shell script file

From Dev

How to loop a shell script across a specific file in all directories?

From Dev

Printing all lines of a file with multiple columns in a shell script

From Dev

Place all sed commands into one shell script file

Related Related

  1. 1

    dividing all numbers in a file using shell scripting

  2. 2

    converting variables in a shell script from strings to numbers

  3. 3

    Adding sequential numbers to the end of file names - Shell Script

  4. 4

    Why is my shell script putting arbitrary numbers in this file?

  5. 5

    Proces a text file shell script that have numbers and strings

  6. 6

    Compare the numbers in text file, if meets the condition run a shell script

  7. 7

    shell script to extract specific columns of a line based on numbers of a file

  8. 8

    export binary file from shell script to browser

  9. 9

    Create , Write and Save file from a Shell script

  10. 10

    Cannot execute a shell script from gradle file

  11. 11

    Reading data from properties file in shell script

  12. 12

    extracting a row from a file using shell script

  13. 13

    linux shell script read from stdin if no file

  14. 14

    Pass variable from shell script to config file

  15. 15

    How to register log file from shell script

  16. 16

    Create , Write and Save file from a Shell script

  17. 17

    Reading data from properties file in shell script

  18. 18

    Extract filename from file using shell script

  19. 19

    Script Shell: Delete a line from a file

  20. 20

    Extract line from a file in shell script

  21. 21

    Writing commands to a file from a shell script

  22. 22

    Extract values from file using shell script

  23. 23

    Shell script read separately columns from file

  24. 24

    Read all the numbers from line of file

  25. 25

    Help with script reading numbers from a file and determining if they are even or odd numbers

  26. 26

    Place all sed commands into one shell script file

  27. 27

    How to loop a shell script across a specific file in all directories?

  28. 28

    Printing all lines of a file with multiple columns in a shell script

  29. 29

    Place all sed commands into one shell script file

HotTag

Archive