Linux Bash ottenere la somma di numeri interi da file linea per linea
Due righe di codice bash per sommare insieme piu’ numeri all’interno di un file.
Prendiamo un file del tipo:
3443535
9878977
67554
987798
232324
Come ottenere la somma totale di tutte le linee? Niente di piu’ semplice:
#!/bin/bash
if [ -z $1 ]; then
echo “Usage: $0 <file>”
exit 1
fi
sum=`(sed ‘s/^/x+=/’ $1; echo x) | bc`
echo $sum
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
17:31 on September 29th, 2011
Great weblog here! Also your website a lot up very fast! What host are you using? Can I am getting your affiliate hyperlink in your host? I wish my website loaded up as fast as yours lol
16:21 on September 27th, 2011
Hello very cool site!! Guy .. Excellent .. Superb .. I’ll bookmark your web site and take the feeds also?I am satisfied to seek out so many useful info here within the submit, we need work out more techniques in this regard, thank you for sharing. . . . . .
12:30 on August 11th, 2011
Someone necessarily help to make significantly posts I would state. This is the first time I frequented your website page and thus far? I surprised with the analysis you made to create this actual post amazing. Great process!
15:03 on November 22nd, 2010
ok ma se io volessi sommare 000001 + 000002 e avere come risultato 000003 come faccio ?? ? :(
22:42 on December 23rd, 2007
Thanks. Very interesting post
23:54 on November 30th, 2007
interessante, più che altro da integrare con altro, tipo output di altri programmi (come un ‘grep ^ -c *’)
16:59 on November 30th, 2007
[...] I’m addicted to puns. :) This is a note for me, you and Michele: do you want to get the sum of a set of integers? Try this one [...]