Somme dans un fichier

Fermé
herysoa Messages postés 9 Date d'inscription lundi 28 juillet 2014 Statut Membre Dernière intervention 13 novembre 2014 - 13 nov. 2014 à 14:47
dubcek Messages postés 18767 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 5 mars 2025 - 14 nov. 2014 à 16:19
Bonjour,
J'ai un fichier essai.txt avec comme contenu:
ab
bc
cd
ef

je voudai avoir leur somme comme som=ab+bc+cd+ef
Pourriez vous m'aider? comment puis je obtenir cette somme?

Merci d'avance.
Herysoa

4 réponses

dubcek Messages postés 18767 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 5 mars 2025 5 628
14 nov. 2014 à 08:20
hello
$ cat fichier
1
10
100
1000
$ awk '{x+=$1} END {print x}' fichier
1111
0
Salut,

Juste pour le fun ;-))

$ cat plop
1
10
100
1000

$ som=$(tr '\n' '+' < plop)

$ echo "${som%+}"
1+10+100+1000

$ echo "${som%+}" | bc
1111
0
dubcek Messages postés 18767 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 5 mars 2025 5 628
14 nov. 2014 à 10:29
et aussi
$ sed '2,$s/$/+/;$s/$/p/' fichier | dc
1111
0
dubcek Messages postés 18767 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 5 mars 2025 5 628
14 nov. 2014 à 16:19
et et aussi
$ echo $(($(sed '$!s/$/+/' fichier)))
1111
0