Created
March 25, 2012 02:45
-
-
Save rsvp/2190889 to your computer and use it in GitHub Desktop.
colline.sh : convert column to line with optional delimiter string.
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
#!/usr/bin/env bash | |
# bash 3.2.25(1) Ubuntu 7.10 Date : 2012-03-25 | |
# | |
# _______________| colline : convert column to line with optional delimiter. | |
# | |
# Usage: colline [file] [delimiter] | |
# ^space as default; string is acceptable. | |
# ^assuming each line represents one column. | |
# | |
# Dependencies: awk, sed | |
# | |
# Tip: If file is multi-column, consider using my: | |
# <( field ... ) # with process substitution. | |
# | |
# CHANGE LOG LATEST version available: https://bitbucket.org/rsvp/gists/src | |
# 2012-03-25 Rewrite using awk resolving the previous bug | |
# which squeezed spaces and also prevented removal | |
# of the last delimiter in some cases. | |
# 2012-03-24 Change default file to stdin; add case for personal shortcuts. | |
# Add colline to gists repository. | |
# 2010-05-29 Second version eliminates disk usage via echo variable. | |
# 2010-05-28 First version for a very common task with tedious details. | |
# _____ Prelims | |
set -u | |
# ^ unbound (i.e. unassigned) variables are treated as error. | |
# Example of default assignment: arg1=${1:-'foo'} | |
set -e | |
# ^ error checking :: Highly Recommended (caveat: you can't check $? later). | |
# | |
# _______________ :: BEGIN Script :::::::::::::::::::::::::::::::::::::::: | |
arg1=${1:-'-'} | |
arg2=${2:-' '} | |
# ^space as default DELIMITER STRING. | |
# Avoid use of double quote character within any such string | |
# since that conflicts with awk's printf statement. | |
# Remedy: consider quotel in gists repository to preprocess. | |
arg2=${arg2//\~/\\~} | |
# ^^escape all occurences of tilde ~ within delimiter string | |
# else sed will complain due to its internal separator | |
# (ignore awk's warning to stderr). | |
case "$arg1" in | |
# handy personal abbreviations for filenames | |
'p') arg1='/tmp/pye.tmp' ;; | |
esac | |
# __________ AWK within a bash script | |
# Here Document serves as its program file. | |
# Bash variables are recognized, so escape awk field numbers. | |
{ awk -f - <(cat "$arg1") <<EOHereDoc | |
{ printf "%s$arg2", \$0 } | |
# ^here printf does not automatically add newline. | |
# ^at the end of each record \$0, append the delimiter string. | |
END { printf "\n" } | |
EOHereDoc | |
} | sed "s~${arg2}$~~" | |
# ^delete the last unnecessary delimiter from awk output. | |
exit 0 | |
# _______________ EOS :: END of Script :::::::::::::::::::::::::::::::::::::::: | |
# Moral: Do not use echo if string fidelity is crucial. | |
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment