Last active
August 29, 2015 13:57
-
-
Save dualbus/9665803 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
sed '/./!be;s/"/""/g;/[, \t"]/s/.*/"&"/;H;$!d;:e;x;s/\n//;s/\n/,/g' |
This file contains hidden or 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
/./! b exchange | |
# escape double quotes and fields | |
s/"/""/g | |
/[, \t"]/s/.*/"&"/ | |
# append to Hold buffer and restart cycle if it's not the last line | |
H | |
$!d | |
:exchange | |
x | |
s/\n// | |
s/\n/,/g |
This file contains hidden or 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/sh | |
set -ux | |
unset script | |
trap 'rm -f "$script"' EXIT | |
script=$(mktemp) | |
oneliner() { | |
sed '/./!be;s/"/""/g;/[, \t"]/s/.*/"&"/;H;$!d;:e;x;s/\n//;s/\n/,/g' | |
} | |
script() { | |
sed -f "$script" | |
} | |
cat <<'eof' > "$script" | |
/./! b exchange | |
# escape double quotes and fields | |
s/"/""/g | |
/[, \t"]/s/.*/"&"/ | |
# append to Hold buffer and restart cycle if it's not the last line | |
H | |
$!d | |
:exchange | |
x | |
s/\n// | |
s/\n/,/g | |
eof | |
for command in oneliner script; do | |
o=$("$command" <<eof | |
a b c | |
c d e | |
123 | |
eof | |
) | |
[ "$o" = '"a b c","c d e" | |
123' ]; echo $? | |
o=$("$command" <<eof | |
1 2 3 | |
eof | |
) | |
[ "$o" = '"1 2 3"' ]; echo $? | |
o=$("$command" <<eof | |
1, 2 3 | |
eof | |
) | |
[ "$o" = '"1, 2 3"' ]; echo $? | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment