Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active August 11, 2017 05:11
Show Gist options
  • Save ksomemo/5844324 to your computer and use it in GitHub Desktop.
Save ksomemo/5844324 to your computer and use it in GitHub Desktop.
1行に数値のみを記載されたテキストの合計値を出力する
#!/bin/sh
sum=0
while read line
do
sum=`expr $sum + $line`
done < num.txt
#/bin/bash
cat <<EOF > num.txt
1
2
3
4
EOF
awk '{v+=$1} END {print v}' nums.txt
@ksomemo
Copy link
Author

ksomemo commented Jun 23, 2013

shellでは数値計算をexprを使って行う。
lineが数として変換できないとエラーとなる。
空行でもエラーとなるので注意する。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment