[wellylug] curly csv formatting

jumbophut jumbophut at gmail.com
Tue Jul 13 09:29:59 NZST 2004


On Mon, 12 Jul 2004 23:53:13 +1200, Mark Signal wrote:
> You mean you can understand that? - it looks like you fell asleep on the
> keyboard....

Actually, I might have.  As Chris might say -- friends don't let
friends program after 11pm.

Corrected scripts here (tested on a bit more data this time):

_________________

: a
/^[ \t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)$/b b
/^[ \t]*,[ \t]*,[ \t]*,\(.*\)$/b c
q
: b
/^[ \t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)$/p
s/^[ \t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)$/\1,\2,\3/
h
n
b a
: c
s/^[ \t]*,[ \t]*,[ \t]*\(,.*\)$/\1/
H
x
s/\n//
p
s/^[ \t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)/\1,\2,\3/
h
n
b a
_________________

/^[ \t]*[^,]\+,/ {p;
s/^[\t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)$/\1,\2,\3/; h; }
/^[ \t]*,[ \t]*,/ {s/^[ \t]*,[ \t]*,[ \t]*\(,.*\)$/\1/; H; x; s/\n//;
p; s/^[\t]*\([^,]\+\),\([^,]\+\),\([^,]\+\),\(.*\)$/\1,\2,\3/; h; }

And an awk one for good measure (just run from command line):

cat /tmp/test4 | awk 'BEGIN {FS=","; OFS=","} $1 ~ /^[ \t]*[^,]+/
{tmp1=$1; tmp2=$2; tmp3=$3;}; $1 ~ /^[ \t]*/ {$1=tmp1; $2=tmp2;
$3=tmp3;}; {print};'

Isn't GNU/Linux a wonderful thing?

Cheers
Tony




More information about the wellylug mailing list