col-totaler.sh

来自「Shall高级编程」· Shell 代码 · 共 56 行

SH
56
字号
#!/bin/bash# Adds up a specified column (of numbers) in the target file.ARGS=2E_WRONGARGS=65if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.then   echo "Usage: `basename $0` filename column-number"   exit $E_WRONGARGSfifilename=$1column_number=$2#  Passing shell variables to the awk part of the script is a bit tricky.#  One method is to strong-quote the Bash-script variable#+ within the awk script.#     $'$BASH_SCRIPT_VAR'#      ^                ^#  This is done in the embedded awk script below.#  See the awk documentation for more details.# A multi-line awk script is invoked by:  awk ' ..... '# Begin awk script.# -----------------------------awk '{ total += $'"${column_number}"'}END {     print total}     ' "$filename"# -----------------------------# End awk script.#   It may not be safe to pass shell variables to an embedded awk script,#+  so Stephane Chazelas proposes the following alternative:#   ---------------------------------------#   awk -v column_number="$column_number" '#   { total += $column_number#   }#   END {#       print total#   }' "$filename"#   ---------------------------------------exit 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?