Discussion:
[shell-script] Somar números de um arquivo
Carlos Peixoto Costa Peixoto cpeix11@yahoo.com.br [shell-script]
2016-08-22 16:31:52 UTC
Permalink
Tenho um arquivo com números reais, sendo somente um por linha. Como faço para extrair o total?
Ex:1.23 sdfa4.55vvvvsfgweg9.23sdfgs 4.78 gh
Grato a todos
Raul Libório rauhmaru@gmail.com [shell-script]
2016-08-22 17:28:24 UTC
Permalink
Carlos Peixoto, usando a mesma lógica que o Júlio colocou no desafio
anterior, respondo o seu:

$ cat file.txt
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh

$ echo "scale=3; $(sed 's/[Aa-zZ]//g;s/ //g' file.txt | tr '\n' '+') 0" | bc
19.79


Abraços

Raul Libório
http://rauhmaru.blogspot.com/
openSUSE Member | Linux User #4444581

/etc/httpd/conf.d/ssl.conf:7
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
Post by Carlos Peixoto Costa Peixoto ***@yahoo.com.br [shell-script]
Tenho um arquivo com números reais, sendo somente um por linha. Como faço
para extrair o total?
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh
Grato a todos
Fredi Rolf Bieging fredi.bieging@gmail.com [shell-script]
2016-08-22 18:24:45 UTC
Permalink
Usando o grep pra pegar os números e o paste pra botar o + entre eles:

grep -Eo '[0-9]+\.?[0-9]*' arquivo | paste -s -d+ | bc
Post by Raul Libório ***@gmail.com [shell-script]
Carlos Peixoto, usando a mesma lógica que o Júlio colocou no desafio
$ cat file.txt
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh
$ echo "scale=3; $(sed 's/[Aa-zZ]//g;s/ //g' file.txt | tr '\n' '+') 0" | bc
19.79
Abraços
Raul Libório
http://rauhmaru.blogspot.com/
openSUSE Member | Linux User #4444581
/etc/httpd/conf.d/ssl.conf:7
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
2016-08-22 13:31 GMT-03:00 Carlos Peixoto Costa Peixoto
Post by Carlos Peixoto Costa Peixoto ***@yahoo.com.br [shell-script]
Tenho um arquivo com números reais, sendo somente um por linha. Como faço
para extrair o total?
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh
Grato a todos
itamarnet@yahoo.com.br [shell-script]
2016-08-22 18:29:46 UTC
Permalink
Caríssimo Carlos

uma alternativa também seria


awk '{ gsub(/[^0-9.]/,""); soma+=$0 }; END { print soma }' file.txt





[]'s
Itamar
Gmail t.collons@gmail.com [shell-script]
2016-08-22 22:57:55 UTC
Permalink
Caro Raul


file.txt = 2.000.000 linhas


Se você usa o sed mais tr, quando uma linha non te numero você obtê un
error.

***@elnegre:~$ time echo "scale=10; $(sed 's/[Aa-zZ]//g;s/ //g'
file.txt | tr '\n' '+') 0" | bc
(standard_in) 1: syntax error

real 0m14.987s
user 0m13.460s
sys 0m1.432s

A melhor opção e:


*/When you use sed & tr, if one line don't have numbers, you have error./*

/*Better option is:*/


echo "scale=10; $(sed 's/[Aa-zZ]//g;s/ //g' file.txt | paste -s -d+) 0" | bc


Outro problema com o meu sed, e que arquivos com mais de 2.000.000 de
linhas tem error.

*/My sed can not file more than 2,000,000 lines./*


S.O = UBUNTU 14.04

Pere

***@gmail.com <mailto:***@gmail.com>



<http://www.imatge.com/>

------------------------------------

AVIS LEGAL

Nota de confidencialitat: Aquest missatge podria contenir missatges
d'empresa i altre informació confidencial tutelada per les lleis de
Europea i altres estats, Si VostÚ el rep per error o sense ser una de
les persones esmenades a la capçalera, haurà de destruir-lo sense
copiar-lo ni revelar-l'ho o qualsevol altre acció relacionada amb el
contingut del missatge i notificar-ho per correu electrònic a l'emissor
del missatge.

Les persones o entitats que incompleixin els deures de confidencialitat
podran ésser perseguides davant dels Tribunals de Justícia sota l'empar
de la legislació civil, penal i administrativa nacional i internacional.

Gracies per la seva cooperació.

------------------------------------

LEGAL NOTICE

Confidentiality notice: This mail may include business and other
confidential information protected under the laws of and other
territories. if you are not one of the intended recipients of the
message, you are legally obliged to delete the message content.
Furthermore, you are kindly requested to report immediately the incident
to the sender.

Breach of confidentiality duties is a serious criminal offence in and
other territories. Offenders shall be legally prosecuted worldwide.

Thank you for your cooperation.
Post by Raul Libório ***@gmail.com [shell-script]
Carlos Peixoto, usando a mesma lógica que o Júlio colocou no desafio
$ cat file.txt
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh
$ echo "scale=3; $(sed 's/[Aa-zZ]//g;s/ //g' file.txt | tr '\n' '+') 0" | bc
19.79
Abraços
Raul Libório
http://rauhmaru.blogspot.com/
openSUSE Member | Linux User #4444581
/etc/httpd/conf.d/ssl.conf:7
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
2016-08-22 13:31 GMT-03:00 Carlos Peixoto Costa Peixoto
Tenho um arquivo com números reais, sendo somente um por linha.
Como faço para extrair o total?
1.23 sdfa
4.55vvvv
sfgweg9.23
sdfgs 4.78 gh
Grato a todos
Raul Libório rauhmaru@gmail.com [shell-script]
2016-08-22 23:05:37 UTC
Permalink
Post by Gmail ***@gmail.com [shell-script]
echo "scale=10; $(sed 's/[Aa-zZ]//g;s/ //g' file.txt | paste -s -d+) 0" | bc
Amigo, na hora eu pensei em fazer mesmo usando o paste, mas deu um branco
na sintaxe de jogar o +. Usei o tr e funcionou - nesse exemplo enviado pelo
nosso colega.
Mas bom saber disso. Nunca peguei essa situação.

Abraço,


Raul Libório
http://rauhmaru.blogspot.com/
openSUSE Member | Linux User #4444581

/etc/httpd/conf.d/ssl.conf:7
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.

Loading...