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:

  1. http://sed.sourceforge.net/grabbag/tutorials/do_it_with_sed.txt
  2. http://www.grymoire.com/Unix/Sed.html
Sphere: Related Content