Skip to content

Instantly share code, notes, and snippets.

@atomicstack
Created October 31, 2011 13:41
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 atomicstack/1327510 to your computer and use it in GitHub Desktop.
Save atomicstack/1327510 to your computer and use it in GitHub Desktop.
Convert a single column list into an SQL IN statement, or alternatively a pipe-separated list which can be used with grep.
#!/bin/bash
export SEP_CHAR=""
export QUOTE_CHAR=""
test "$1" == "--grep" && export SEP_CHAR="|"
( test "$1" == "--sql" || test -z "$1" ) && export SEP_CHAR="," && export QUOTE_CHAR="'"
new_list=$( pbpaste | perl -e 'chomp(@lines = <STDIN>); print q{(} . join( $ENV{SEP_CHAR} => map { $ENV{QUOTE_CHAR} . $_ . $ENV{QUOTE_CHAR} } @lines ) . q{)}' )
echo $new_list | pbcopy
echo "$new_list"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment