Created
June 7, 2013 13:19
-
-
Save geoom/5729183 to your computer and use it in GitHub Desktop.
For those scripts needing a single do-it-all tool, a Swiss army knife, there is Perl. Perl combines the capabilities of sed and awk, and throws in a large subset of C, to boot. It is modular and contains support for everything ranging from object-oriented programming up to and including the kitchen sink. Short Perl scripts lend themselves to emb…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adds up a specified column (of numbers) in the target file. | |
# Floating-point (decimal) numbers okay, because awk can handle them. | |
ARGS=2 | |
E_WRONGARGS=85 | |
if [ $# -ne "$ARGS" ] # Check for proper number of command-line args. | |
then | |
echo "Usage: `basename $0` filename column-number" | |
exit $E_WRONGARGS | |
fi | |
filename=$1 | |
column_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 here 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment