Talk is cheap, show me the code
Given this text stream:
SELECT md.Access, md.Allowance FROM SomeTable md ;
I want to translate "table.FieldName" to "table.FieldName AS FieldName"
Using this script below:
#!/bin/sed -rf
# Transfrom table.FieldName to table.FieldName AS FieldName
# sed sux!
# :foo
# {
# s/\n/ /g
# s/\t/ /g
# s/\s+/ /g
# N
# bfoo
# }
:control
{
/^SELECT$/{
d
n
}
/^FROM*/{
N
b_dropall
}
/./ {
#central processing
#N
#s/\n/ /g # remove \n
#s/\s+/ /g # keep one space only
#s/^\s+//g # throw preceeding spaces
b_cpu
}
}
b_end
:_dropall
{
N
d
b_dropall
}
:_cpu
#s/([\t\s]*(.*?)\.?(.*?))\,?\n/\2 AS \2/g
#s/([\t\ ]+)(((.*?)?(\.?(.*?))+)\,?)+/\0 AS \4/g
#s/^\s*(.*(\.(.*))){1}(\,)?/\0|\1|\2|\3|\4/g
s/^\s*(((.*?)\.)?((.*?)))\,?/\0|\1|\2|\3|\4|\5/g
I managed to get:
[geek@toni GTFW]$ ./postgres.sed < ./test
md.Access,|md.Access,|md.|md|Access,|Access,
md.Allowance|md.Allowance|md.|md|Allowance|Allowance
You see, I don’t want a greedy match, but anyhow I still got it. Now where those people who claims nothing better than sex^Hd?!
Reference:
This section gives me most headache. But experience told me that this blog mostly contains personal ramblings related to daily life, open source, and web 2.0. Recently, it turns out to be an idea-box where you can found ideas you can execute in your new startup ;). My ideas and opinions are not bullet proof and never intended to be one way stream. Kindly participate to improve your and my perspective regarding any particular post. Welcome aboard and enjoy your stay
Leave a reply