Skip to content

Instantly share code, notes, and snippets.

@acro5piano
Last active June 25, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acro5piano/a0a9fcb2a58cc6805e6987122155e703 to your computer and use it in GitHub Desktop.
Save acro5piano/a0a9fcb2a58cc6805e6987122155e703 to your computer and use it in GitHub Desktop.
CSVファイルから特定の列を取り出す自作関数 ref: http://qiita.com/acro5piano/items/26c4926b243e615a2d77
cat <<EOF > test.txt
a b c
d e f
EOF
# cutを使う場合
cat test.txt | cut -d ' ' -f 1,3
#=> a c
#=> d f
# awkで同じことをする
cat test.txt | awk '{print $1, $3}'
#=> a c
#=> d f
colprint(){
options=''
while getopts F: OPT; do
options="$options -$OPT $OPTARG"
done
shift $((OPTIND - 1))
command=''
for str in $@; do
command=`echo $command '$'$str, `
done
awk $options "{print `echo $command | sed 's/,$//g'`}"
}
cat <<EOF | colprint 1 3
a b c
d e f
EOF
#=> a c
#=> d f
cat <<EOF | colprint -F ,, 1 3
a,,b,,c
d,,e,,f
EOF
#=> a c
#=> d f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment